Let's begin.
Create a new Windows Forms application.
![]()
Create a database (named Sample). Add a table, tbl_Employee. The following is the table schema for creating tbl_Employee.
Create a form (named frmSearch) and Drop Label, TextBox and DataGridView control from the ToolBox.
![]()
Now, go to the frmSearch.cs code and add the System.Data and System.Data.SqlClient namespaces. The following is the frmSearch.cs code:
- using System;
- using System.Data;
- using System.Windows.Forms;
- using System.Data.SqlClient;
-
- namespace SearchRecord
- {
- public partial class frmSearch : Form
- {
-
- string cs = "Data Source=.;Initial Catalog=Sample;Integrated Security=true;";
- SqlConnection con;
- SqlDataAdapter adapt;
- DataTable dt;
- public frmSearch()
- {
- InitializeComponent();
- }
-
- private void frmSearch_Load(object sender, EventArgs e)
- {
- con = new SqlConnection(cs);
- con.Open();
- adapt = new SqlDataAdapter("select * from tbl_Employee",con);
- dt = new DataTable();
- adapt.Fill(dt);
- dataGridView1.DataSource = dt;
- con.Close();
- }
-
- private void txt_SearchName_TextChanged(object sender, EventArgs e)
- {
- con = new SqlConnection(cs);
- con.Open();
- adapt = new SqlDataAdapter("select * from tbl_Employee where FirstName like '"+txt_SearchName.Text+"%'", con);
- dt = new DataTable();
- adapt.Fill(dt);
- dataGridView1.DataSource = dt;
- con.Close();
- }
- }
- }
In the preceding code, we have created a frmSearch_Load event for displaying the tbl_Employee data in the DataGridView when the form loads.
The txt_SearchName_TextChanged event fires when the text of the txt_SearchName TextBox changes.
Final Preview
![]()
I hope you will like it. Thanks.