Home All Groups Group Topic Archive Search About
Author
24 Jan 2006 11:12 AM
Zoltan Hubai
Hi

I'm trying to do incremental search in my DataTable with TextBox and
DataGridView. I did it but like to know if it's there a simple method to
do the same.

I have a DataSource with more DataTables. Depending wich TextBox has the
focus I do the incremental search on the related DataTable to the
TextBox. The DataTable is always displayed in the same DataGridView.

I put on a Form a BindingSource. The DataGridView is linked to the
BindingSource. When the TextBox control get's the focus i setup the
BindingSource DataSource property to the related DataTable. This works
nice and the related data is displayed in the DataGridView.

Now I created a TextChanged event for the TextBox that does the actual
incremental search:

private void textBox1_TextChanged(object sender, EventArgs e)
{
   // serach the related DataTable for all rows that begins with
   // the TextBox.Text
   DataRow[] rows =
jciSifarnik.Tables["TableName"].Select(string.Format("Sifra LIKE
'{0}*'",textBox1.Text));
   // Do we have any row's that meet the search criteria
   if (rows.Length > 0)
   {
     // we have some rows, find out the index for the first
     int i = bindingSource1.Find("Sifra", rows[0]["Sifra"]);
     if (i >= 0)
       // set the Position for the first row that was found
       bindingSource1.Position = i;
   }
}

This works nice, and the first row that matches the criteria is focused
in the DataGridView.
I just don't like that I need to do 2 times the search. Can I set
somehow the focused row after the first search (Select) or can I maybe
do the incremental searching with the DataGridView without using the
BindingSource control?

Thx

AddThis Social Bookmark Button