In this article I will explain with an example, how to Filter DataGridView using ComboBox in Windows Forms Application using C# and VB.Net.
By default the DataGridView will show all records. As soon as user selects an item in ComboBox, the records will be searched in Database and the DataGridView rows will be filtered in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Form Design
The Form consists of a ComboBox and a DataGridView control.
Filter DataGridView using ComboBox in Windows Forms Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
 
 
Filter DataGridView using ComboBox in Windows Forms Application
The ComboBox is populated with list of Countries using the PopulateComboBox function inside the Form Load event handler.
Note: For more details on populating ComboBox with default item from database, please refer my article, Add (Insert) Default First Value in ComboBox in Windows Forms Application using C# and VB.Net.
 
The DataGridView is populated using the PopulateDataGridView function in two places, first in the Form Load event handler and second in the SelectionChangeCommitted event handler of the ComboBox.
The SQL Query is created in such a way that it will return all records if the default Blank value is selected in ComboBox and it will return filtered records when the ComboBox selection is a valid Country.
Inside the PopulateDataGridView function, the SQL Query is executed and the results are populated into a DataTable which is ultimately supplied to the DataGridView as DataSource.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.PopulateComboBox();
    this.PopulateDataGridView();
}
 
private void cbCountries_SelectionChangeCommitted(object sender, EventArgs e)
{
    this.PopulateDataGridView();
}
 
private void void PopulateComboBox()
{
    string query = "SELECT DISTINCT Country FROM Customers";
    string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
        {
            //Fill the DataTable with records from Table.
            DataTable dt = new DataTable();
            sda.Fill(dt);
 
            //Insert the Default Item to DataTable.
            DataRow row = dt.NewRow();
            row[0] = "";
            dt.Rows.InsertAt(row, 0);
 
            //Assign DataTable as DataSource.
            cbCountries.DataSource = dt;
            cbCountries.DisplayMember = "Country";
            cbCountries.ValueMember = "Country";
        }
    }
}
 
private void PopulateDataGridView()
{
    string query = "SELECT CustomerID, ContactName, Country FROM Customers";
    query += " WHERE Country = @Country";
    query += " OR ISNULL(@Country, '') = ''";
    string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedValue);
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }
    }
}
 
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Me.PopulateComboBox()
    Me.PopulateDataGridView()
End Sub
 
Private Sub cbCountries_SelectionChangeCommitted(sender As System.Object, e As System.EventArgs) Handles cbCountries.SelectionChangeCommitted
    Me.PopulateDataGridView()
End Sub
 
Private Sub PopulateComboBox()
    Dim query As String = "SELECT DISTINCT Country FROM Customers"
    Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123"
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
            'Fill the DataTable with records from Table.
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)
 
            'Insert the Default Item to DataTable.
            Dim row As DataRow = dt.NewRow()
            row(0) = ""
            dt.Rows.InsertAt(row, 0)
 
            'Assign DataTable as DataSource.
            cbCountries.DataSource = dt
            cbCountries.DisplayMember = "Country"
            cbCountries.ValueMember = "Country"
        End Using
    End Using
End Sub
 
Private Sub PopulateDataGridView()
    Dim query As String = "SELECT CustomerID, ContactName, Country FROM Customers"
    query &= " WHERE Country = @Country"
    query &= " OR ISNULL(@Country, '') = ''"
    Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123"
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query, con)
            cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedValue)
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim dt As DataTable = New DataTable()
                sda.Fill(dt)
                dataGridView1.DataSource = dt
            End Using
        End Using
    End Using
End Sub
 
 
Screenshot
Filter DataGridView using ComboBox in Windows Forms Application using C# and VB.Net
 
 
Downloads