Results 1 to 6 of 6

Thread: Search Datagrid

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    7

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. myBindingSource.Filter = String.Format("First_Name LIKE '{0}%'", Me.TextBox1.Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    7

    Re: Search Datagrid

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. myBindingSource.Filter = String.Format("First_Name LIKE '{0}%'", Me.TextBox1.Text)
    Thanks! Code works perfectly! That was easy enough.

  4. #4
    New Member vbbeginner92's Avatar
    Join Date
    Mar 2011
    Location
    Lahore, Pakistan
    Posts
    13

    Re: Search Datagrid

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. 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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
    2.     'Restart the filter delay.
    3.     Me.Timer1.Stop()
    4.     Me.Timer1.Start()
    5. End Sub
    6.  
    7. Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    8.     'Filter the data once only.
    9.     Me.Timer1.Stop()
    10.     Me.BindingSource1.Filter = String.Format("First_Name LIKE '{0}%'", Me.TextBox1.Text)
    11. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    New Member vbbeginner92's Avatar
    Join Date
    Mar 2011
    Location
    Lahore, Pakistan
    Posts
    13

    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
  •  



Click Here to Expand Forum to Full Width