Search within a DataGridView
-
Hi I have a windows app with a datagridview and a textbox On form load I bind the grid with all records in a table. What I want to do is as I type in a textbox I want it to filter the grid to give me only what I have typed so far in the textbox.The seach will only filter on a specific colom and not on the entire grid's coloms. Cna someone help me on this, I do it this way, because I dont want to hit the DB everytime I type a letter in the textbox. Any ideas how to approach this?
-
Hi I have a windows app with a datagridview and a textbox On form load I bind the grid with all records in a table. What I want to do is as I type in a textbox I want it to filter the grid to give me only what I have typed so far in the textbox.The seach will only filter on a specific colom and not on the entire grid's coloms. Cna someone help me on this, I do it this way, because I dont want to hit the DB everytime I type a letter in the textbox. Any ideas how to approach this?
Bind the
DataGridView
to aBindingSource
and use theFilter
property of the BindingSource to filter the results. The Filter property acts just like a WHERE clause in SQL, so you could do;BindingSource.Filter = "ColName Like '" & textbox.text & "'"
You can also then extract the filtered dataset from the source by using the following tip; http://www.codeproject.com/Tips/50317/Access-the-Data-Rows-Filtered-by-the-BindingSource.aspx[^]Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.com -
Bind the
DataGridView
to aBindingSource
and use theFilter
property of the BindingSource to filter the results. The Filter property acts just like a WHERE clause in SQL, so you could do;BindingSource.Filter = "ColName Like '" & textbox.text & "'"
You can also then extract the filtered dataset from the source by using the following tip; http://www.codeproject.com/Tips/50317/Access-the-Data-Rows-Filtered-by-the-BindingSource.aspx[^]Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.comThank you so much, it seems that this will do the trick But the moment I start typing in the textbox, the grid is empty and stays empty.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
BindingSource.Filter = "ColName Like '" & textbox.text & "'"
End Sub
-
Thank you so much, it seems that this will do the trick But the moment I start typing in the textbox, the grid is empty and stays empty.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
BindingSource.Filter = "ColName Like '" & textbox.text & "'"
End Sub
Depending on your data source e.g. SQL; you will need to apply suitable wild cards depending on how you want to search. You need to prefix and/or suffix the query text with a % or an *
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.com -
Thank you so much, it seems that this will do the trick But the moment I start typing in the textbox, the grid is empty and stays empty.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
BindingSource.Filter = "ColName Like '" & textbox.text & "'"
End Sub