0
votes

I've searched around and couldn't find the one I've been looking for.

I have a DataGridView in VB.NET with thousands of records from MySQL Database. Now, I want to SEARCH item_description that matches or something LIKE the inputted text. I am using TEXTCHANGED

I didn't use search to database since its taking time to load.

Sample is

Search: orange

DataGridView must filter and display only with the words like "orange"

ITEM_ID ITEM_DESCRIPTION
120 Orange Juice
832 Orange Fruit

I am thinking of getting the array list names of the column "item_description" and filter it but I don't know where to start and what codes to use. Thank you

1
The grid should be irrelevant. You should query your database and populate a DataTable with the result set, bind that to a BindingSource and bind that to your grid. Both the DataTable and the BindingSource provides means to perfrom simple queries against the data and, for something more complex, you can perform a LINQ query against the DataTable. - jmcilhinney
@jmcilhinney Hi, yes all the data being filtered was from a database. I will try this. Thank you so much for this suggestion. - Synergetic

1 Answers

1
votes

Assuming that you have taken the advice in my comment and populated a DataTable and then bound that to the DataGridView via a BindingSource, filtering the data is a simple matter of setting the Filter property of that BindingSource. In your case, it should be something like this:

myBindingSource.Filter = "ITEM_DESCRIPTION LIKE 'orange*'"

One issue with filtering on the TextChanged event of a TextBox is that you will filter multiple times when the user types multiple characters when you only need the last one. To alleviate that, I suggest using a Timer that you start/restart each time a character is typed and then filter when it Ticks. That will enable the user to type multiple characters without filtering but also not have a significant wait after they stop typing for the filtering to be done. I recommend an interval of around 250 milliseconds but you can experiment to see what you think is best. E.g.

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    'Start/restart the Timer.
    Timer1.Stop()
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Timer1.Stop()

    'Perform the filtering.
    BindingSource1.Filter = $"ColumnName LIKE '{TextBox1.Text}*'"
End Sub