C# DataGridView Sorting and Filtering

The DataGridView control offers a remarkable degree of configurability and extensibility, presenting a wide range of properties, methods, and events that facilitate effortless customization of its appearance and behavior. A DataView serves as a powerful tool for filtering and sorting data within a DataTable, enabling precise control over data presentation. To illustrate this functionality in action, the following C# program demonstrates the implementation of a DataView to filter and sort a DataGridView, showcasing the seamless integration and utilization of these features.

datgridview filter sort in c#

How to sort Datagridview

The DataGridView control in C# offers an inherent feature of automatic sorting, allowing users to effortlessly arrange data within the control by manually sorting any desired column. This versatile functionality empowers users to sort data in either ascending or descending order, based on the contents of the specified column. Additionally, the DataGridView provides a user-friendly experience by visually indicating the sorting operation when a user clicks on the column header. This intuitive behavior ensures a seamless and interactive data sorting experience for users within the DataGridView control.

dataGridView1.Sort(dataGridView1.Columns[1], ListSortDirection.Ascending);
how to datgridview sort in c#

In the above code , datagridview sort the title column thats 1st column.

How to filter Datagridview

There are multiple methods available to filter columns within the DataGridView control, providing users with flexible options to refine and manipulate data. One approach involves sorting the data directly during the retrieval process from the database by utilizing the "order by" clause, ensuring that the desired sorting order is applied right from the source. Alternatively, an alternative method can be employed to accomplish the same objective, as illustrated below.

DataView dv; dv = new DataView(ds.Tables[0], "type = 'business' ", "type Desc", DataViewRowState.CurrentRows); dataGridView1.DataSource = dv;
how to datgridview filter in c#

In the above code, datagridview is filter the column Type and value is Business.

Full Source C#
using System; using System.ComponentModel; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { DataSet ds = new DataSet(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT title_id,title,type,pub_id FROM Titles"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); connection.Open(); dataadapter.Fill(ds, "Titles_table"); connection.Close(); dataGridView1.DataSource = ds.Tables[0]; } private void button1_Click(object sender, EventArgs e) { DataView dv; dv = new DataView(ds.Tables[0], "type = 'business' ", "type Desc", DataViewRowState.CurrentRows); dataGridView1.DataSource = dv; } private void button2_Click(object sender, EventArgs e) { dataGridView1.Sort(dataGridView1.Columns[1], ListSortDirection.Ascending); } } }