In this article I will explain with an example, how to search (filter)
DataGridView on
TextBox KeyPress in Windows Forms (WinForms) Application using C# and VB.Net.
By default, the
DataGridView will show all records. As soon as user starts to type in the
TextBox, 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
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
Search (Filter) DataGridView on TextBox KeyPress in Windows Forms Application
The
DataGridView is populated using the
PopulateDataGridView function in two places, first in the
Form Load event handler and second in the
KeyUp event handler of the
TextBox.
The SQL Query is created in such a way that it will return all records if the
TextBox value is Blank and will return filtered records when the
TextBox has a value.
Inside the
PopulateDataGridView function, the SQL Query is executed and the results are populated into a
DataTable.
C#
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = this.PopulateDataGridView();
}
private void txtName_KeyUp(object sender, KeyEventArgs e)
{
dataGridView1.DataSource = this.PopulateDataGridView();
}
private DataTable PopulateDataGridView()
{
string sql = "SELECT CustomerID, ContactName, Country FROM Customers";
sql += " WHERE ContactName LIKE '%' + @ContactName + '%'";
sql += " OR @ContactName = ''";
string constr = @"Data Source=.\SQL2019;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddWithValue("@ContactName", txtName.Text.Trim());
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
dataGridView1.DataSource = Me.PopulateDataGridView()
End Sub
Private Sub txtName_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyUp
dataGridView1.DataSource = Me.PopulateDataGridView()
End Sub
Private Function PopulateDataGridView() As DataTable
Dim sql As String = "SELECT CustomerID, ContactName, Country FROM Customers"
sql &= " WHERE ContactName LIKE '%' + @ContactName + '%'"
sql &= " OR @ContactName = ''"
Dim constr As String = "Data Source=.\SQL2019;Initial Catalog=Northwind;User ID=sa;Password=pass@123"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@ContactName", txtName.Text.Trim())
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot
Downloads