How to Search DataGridView by using TextBox in C#
By FoxLearn 11/22/2024 8:04:57 AM 25.54K
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 Print Text in a Windows Form Application Using C#
- How to fill ComboBox and DataGridView automatically in C#
- How to Read text file and Sort list in C#
- How to pass ListView row data into another Form in C#
- How to read and write to text file in C#
- How to make a Countdown Timer in C#
- How to Display selected Row from DataGridView to TextBox in C#
- How to Get all Forms and Open Form with Form Name in C#