|
-
Oct 7th, 2010, 08:24 PM
#1
Thread Starter
New Member
Search Datagrid
I want to be able to type text in a search box (textbox1) and have the datagrid adjust at the same time. My datagridview is called DGEstudent and the data source is dataset2 and is pulling from the student table. As soon as I start typing in the textbox my datagrid just goes blank. Any help would be greatly appreciated.
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.KeyUp
Dim dv As DataView = New DataView()
dv.Table = DataSet2.Student
dv.RowFilter = "First_Name like '" & TextBox1.Text & "%'"
DGEstudent.DataSource = dv
End Sub
-
Oct 7th, 2010, 09:28 PM
#2
Re: Search Datagrid
Don't change the DataSource of your grid. It looks like you are using a typed DataSet so I assume that you are binding the grid in the designer, correct? If so then you should also have a BindingSource, where the DataTable is bound to the BindingSource and the BindingSource is bound to the grid. It's exactly for this sort of thing that the BindingSource exists. You should be handling the TextChanged event of your TextBox and doing this:
vb.net Code:
myBindingSource.Filter = String.Format("First_Name LIKE '{0}%'", Me.TextBox1.Text)
-
Oct 7th, 2010, 10:17 PM
#3
Thread Starter
New Member
Re: Search Datagrid
 Originally Posted by jmcilhinney
Don't change the DataSource of your grid. It looks like you are using a typed DataSet so I assume that you are binding the grid in the designer, correct? If so then you should also have a BindingSource, where the DataTable is bound to the BindingSource and the BindingSource is bound to the grid. It's exactly for this sort of thing that the BindingSource exists. You should be handling the TextChanged event of your TextBox and doing this:
vb.net Code:
myBindingSource.Filter = String.Format("First_Name LIKE '{0}%'", Me.TextBox1.Text)
Thanks! Code works perfectly! That was easy enough.
-
Mar 4th, 2011, 10:09 AM
#4
New Member
Re: Search Datagrid
 Originally Posted by jmcilhinney
Don't change the DataSource of your grid. It looks like you are using a typed DataSet so I assume that you are binding the grid in the designer, correct? If so then you should also have a BindingSource, where the DataTable is bound to the BindingSource and the BindingSource is bound to the grid. It's exactly for this sort of thing that the BindingSource exists. You should be handling the TextChanged event of your TextBox and doing this:
vb.net Code:
myBindingSource.Filter = String.Format("First_Name LIKE '{0}%'", Me.TextBox1.Text)
OH EM G!!! thanx for this info dude!! i wanted to do a grid search, was trying since yesterday!!!!!!
THAAANX!!!! changed my line like you say above youre my ***** hero man!!!! rock on!! =D
-
Mar 5th, 2011, 02:34 AM
#5
Re: Search Datagrid
I will add one more point that will complicate the code a little but makes for a better user experience. If you handle the TextChanged event and update the Filter property there, you will be re-filtering the data every time the user types a character. If the user wants to type several characters, they don't need the data re-filtered until they have type them all. For that reason, it's not a bad idea to use a Timer to create a delay between typing and filtering. That way you can re-start the delay each time a character is typed. This allows the user to type several characters and defer the filtering until the end. Here's some example code:
vb.net Code:
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged 'Restart the filter delay. Me.Timer1.Stop() Me.Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick 'Filter the data once only. Me.Timer1.Stop() Me.BindingSource1.Filter = String.Format("First_Name LIKE '{0}%'", Me.TextBox1.Text) End Sub
You might have to experiment a bit to get the Interval of the Timer right. 500 (half a second) might be a good starting point but you might want more for slow typists and less for fast. You might even include that in your program options.
-
Mar 5th, 2011, 11:36 AM
#6
New Member
Re: Search Datagrid
^ thanx! useful!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|