DataGridView Sorting/Filtering in VB.NET

The DataGridView control offers a highly versatile and configurable table that serves as an ideal platform for displaying data in a visually appealing manner. Its extensive customization capabilities enable developers to imbue their applications with tailored behaviors, empowering them to create unique and efficient solutions.

One valuable feature that enhances data management within the DataGridView is the ability to employ a DataView object. This object provides a powerful mechanism for filtering and sorting data within a DataTable, further augmenting the control's functionality.

datgridview filter sort vb.net

How to sort Datagridview - vb.net

The DataGridView control offers a remarkable feature of automatic sorting, enabling seamless sorting of any column within the control. This functionality empowers you to effortlessly arrange the data in ascending or descending order, based on the contents of the designated column using the sort() method.

By using this built-in sorting capability, you can efficiently organize the data within the DataGridView control, enhancing its usability and ensuring a streamlined user experience. The sort() method provides a straightforward and intuitive means to accomplish this task, allowing you to effortlessly configure the desired sorting order and swiftly adapt the presentation of data to meet your specific requirements.

DataGridView1.Sort(DataGridView1.Columns(1), ListSortDirection.Ascending)
datgridview sort vb.net

In the above vb.net code , datagridview sort the title column.

How to filter Datagridview - vb.net

There are multiple approaches available for filtering a DataGridView column. One option is to apply sorting directly during the data retrieval process from the database by utilizing the ORDER BY clause in the SQL statement. This enables you to obtain the data in the desired order, which can subsequently be displayed in the DataGridView.

Alternatively, you can employ the following method to filter a DataGridView column:

  1. Retrieve the data from the database and populate the DataGridView.
  2. Utilize the built-in capabilities of the DataGridView control to perform the filtering.
Dim dv As DataView dv = New DataView(ds.Tables(0), "type = 'business' ", "type Desc", DataViewRowState.CurrentRows) DataGridView1.DataSource = dv
datgridview filter vb.net

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

Full Source VB.NET
Imports System.Data.SqlClient Imports System.ComponentModel Public Class Form1 Dim ds As New DataSet Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True" Dim sql As String = "SELECT title_id,title,type,pub_id FROM Titles" Dim connection As New SqlConnection(connectionString) Dim dataadapter As New SqlDataAdapter(sql, connection) connection.Open() dataadapter.Fill(ds, "Titles_table") connection.Close() DataGridView1.DataSource = ds.Tables(0) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim dv As DataView dv = New DataView(ds.Tables(0), "type = 'business' ", "type Desc", DataViewRowState.CurrentRows) DataGridView1.DataSource = dv End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DataGridView1.Sort(DataGridView1.Columns(1), ListSortDirection.Ascending) End Sub End Class