Data Grid View Sorting & Filtering

In this blog, we will code how to Filter and Sort data present in DataGrid.

The DataGridView Controls provides a flexible and customized way to display rows and columns.

Programming

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace DatagridViewSortingFiltering
{
    public partial class Form1 : Form
    {
        int _salary = 0;
        string _ordering = "Asc";
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
           
            ///<Checking for Valid Salary Input>
            bool valid;
            valid = int.TryParse(txt_Salary.Text, out _salary);

            if (!valid)
            {
                MessageBox.Show("Please Salary the Salary amount", "Invalid Input by User", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                _salary = Convert.ToInt16(txt_Salary.Text);
            }
            ///</Checking for Valid Salary Input>

            //Sql Server Specific Connection String to Connect Database
            string connectionString = @"Data Source=L04WEBMET001\MSSQL2005;Initial Catalog=LGTRD;User id =LG_MssDev;Password=LG@Dev765";
            string sql = "SELECT * FROM EMP";
            //Creating SqlConnection object 
            SqlConnection conn = new SqlConnection(connectionString);
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            conn.Open();
            //Filling the DataSet using the DataAdapter Fill Method
            da.Fill(ds, "Employee");
            conn.Close();

            //Creating a DataView Object for Sorting, Filtering Operations
            DataView dv;
            dv = new DataView(ds.Tables[0], "Sal >= " + _salary.ToString(), "Sal " + _ordering, DataViewRowState.CurrentRows);
            //Binding the DataGridview dgLoadDetails 
            dgLoadDetails.DataSource = dv;
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmb_Ordering.Items.Add("Ascending");
            cmb_Ordering.Items.Add("Descending");
            cmb_Ordering.SelectedIndex = -1;
        }

        private void cmb_Ordering_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_Ordering.Text == "Ascending")
                _ordering = "Asc";
            else
                _ordering = "Desc";
        }
    }
}



The int.TryParse method provides a good approach to validate Input.

The TryParse method first tries to convert the string Input to Integer and returns a Boolean (true/false)

If the boolean value results to false the Input is Invalid i.e., It cannot convert the string Input to Integer Here (int.TryParse)
If resulting in true the Input is a valid one.

Thanks for Reading.
Ebook Download
View all
Learn
View all