How to Search DataGridView by using TextBox in C#
By FoxLearn 11/22/2024 8:04:57 AM 25.41K
How to Filter and Search a DataGridView using a TextBox in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SearchDataGridView" and then click OK
Drag and drop the Label, TextBox, DataGridView controls from the Visual Toolbox onto your form designer, then design your form as shown below.
We begin by defining a connection string in the app.config
file, allowing the application to connect to a SQL Server database.
<configuration> <connectionStrings> <add name="cn" connectionString="data source=.;initial catalog=NORTHWND;user id=sa;password=123@qaz" providerName="System.Data.SqlClient"/> </connectionStrings> </configuration>
Next, we set up a Form1
with a DataGridView
and a TextBox
for the search functionality.
The form loads data from the Products
table of the SQL Server database using a SqlDataAdapter
and binds the data to the DataGridView
.
private void Form1_Load(object sender, EventArgs e) { try { using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString)) { if (cn.State == ConnectionState.Closed) cn.Open(); using (SqlDataAdapter da = new SqlDataAdapter("select *from products", cn)) { //Fill data to datatable da.Fill(dt); dataGridView.DataSource = dt; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
A key feature of this example is enabling the search functionality. As the user types into the TextBox
and presses the Enter key, the DataGridView
is filtered in real time. The RowFilter
property of the DataView
is used to search for products that match the text entered in the TextBox
.
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13)//Enter key { DataView dv = dt.DefaultView; //Filter datagridview using textbox dv.RowFilter = string.Format("productname like '%{0}%'", txtSearch.Text); dataGridView.DataSource = dv.ToTable(); } }
You need to define a DataTable to help you fill data from SQL Server into the DataTable, as shown below.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SearchDataGridView { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable dt = new DataTable("Products"); } }
This example allows users to search and filter records in the DataGridView based on the text entered into the TextBox, making it easy to find specific items in the database. By utilizing SqlDataAdapter and DataView.RowFilter, this approach ensures that the filtering operation is quick and responsive.
VIDEO TUTORIAL
- How to make a File Explorer in C#
- How to create multi language using Resource Manager and Culture Info in C#
- How to Load selected columns data in DataGridView in C#
- How to use Timer control in C#
- How to read .rtf file in C#
- How to read text file (*.txt) in C#
- How to make an Alarm clock in C#
- How to Save and Retrieve Image from SQL database in C#