Instead of using the
TextChanged
event try using the
textBox1_Validating
event. It will fire when the user has finished typing and tries to move away from the text box. That way you will only be searching the database once for the whole string entered
i.e. In the IDE Properties window for the textbox click on the "Lightening bolt" icon to get a list of possible events for the textbox.
Scroll down the list until you find
Validating
and double-click it.
Move the code currently in your
textBox1_TextChanged
event into the new event
textBox1_Validating
event
You should end up with something like this
private void textBox1_Validating(object sender, CancelEventArgs e)
{
Project par = new Project();
DataTable tabl = par.SearchProjeh(txtFamilSearch.Text.Trim());
if (tabl != null && tabl.Rows.Count > 0)
{
dgvSearch.DataSource = tabl;
dgvSearch.Visible = true;
}
else
{
dgvSearch.DataSource = null; dgvSearch.Visible = false;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
NOTE I have removed the Try-catch block ... if you are not going to do something with an exception then don't catch it... it's very easy to "swallow" exceptions or lose valuable information about them.