Search code examples
vb.netfilterdatagridviewtextbox

Filter multiple Datagridview with a Textbox without using SQL


I'd like to filter a DataGridView using a Textbox to find matches in an existing column defined by a ComboBox that has all column name in a collection.

For instance, if I choose "Name" in the ComboBox and then Enter "The S" in the Textbox, the DataGridView only show rows where the column Name value starts with "The S", so I'll get "The StackOverFlow", "The Steak", and so on.

The thing is, I don't want to launch an SQL request everytime I filter, so I'll save ressources.

Here is my actual code where

  • ListProd is my DataGridView
  • CBFiltre is my ComboBox
  • tbRefChantier is my TextBox
  • ClassTables.Produits is my DataSet that have EVERY entry from my MariaDB database

    Private Sub tbRefChantier_TextChanged(sender As Object, e As EventArgs) Handles tbRefChantier.TextChanged
    
        Try
    
            ListeProd.DataSource = ClassTables.Produits.Tables("Produits").Select(CbFiltre.Text & " Like '%" & tbRefChantier.Text & "%'")
    
        Catch Exc As Exception
            MsgBox("Erreur logiciel :" & Chr(10) & Chr(10) & Exc.Message)
    
        End Try
    End Sub
    

But when I try to Filter it, my DataGridView only shows

A screenshot of a filtered DataGridView, with no requirement in the filter

But it seems to actually filter since the number of rows changes when I type something


Solution

  • Thanks to the help of @jmcilhinney I worked that out.

    I ended up creating, in my form

    Dim BindingData As New BindingSource

    Then

    BindingData.DataSource = Class.MyDataSetName.Tables("NameOfTheTable") TheDataGridView.DataSource = BindingData

    And on Textbox.TextChange

    Try BindingData.Filter = ComboBoxFilter.Text & " Like '%" & TextBoxFilter.Text & "%'" TheDataGridView.DataSource = BindingData Catch Exc As Exception MsgBox("Erreur logiciel :" & Chr(10) & Chr(10) & Exc.Message) End Try