If you can load all contacts at once, then its simple. On form load get all contacts and save them in DataView
. Then bind grid to this view:
DataView contactsView;
private void Form2_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
var da = new SqlDataAdapter("SELECT * FROM from contactsinfo", conn);
da.Fill(dt);
}
contactsView = dt.AsDataView();
dataGridView1.DataSource = contactsView;
}
Then just change row filter of DataView
when text changes in filter TextBox:
private void textBox1_TextChanged(object sender, EventArgs e)
{
contactsView.RowFilter =
String.Format("ContactName LIKE '{0}%'", textBox1.Text);
}
If you can't pre-load all data, then you should use same TextChanged
event to query for filtered data.
NOTE: You should handle '
in user input.