Filter on datagrid view

manuel angeli 40 Reputation points
2023-09-25T13:43:55.2733333+00:00

ciao a tutti!!!!!!

with the following code I populate a datagridview by taking data from a mysql database.
is there any way to filter the data (as I type on a textbox see the filter in real time) without using a datatable?
Or can I pass the contents of the datagrid view inside a data table?

Thanks in advance

Dim cnString As String
Dim sqlQRY As String
Dim conn As Common.DbConnection
Dim da As Common.DbDataAdapter
Dim ds As DataSet = New DataSet
cnString = datasource=192.168.1.10;port=3307;SslMode=none;username=username;password=password!;database=ordini_fornitori"
sqlQRY = "Select `id`, `fornitore`, `articolo`,`cliente`, `scheda`, `colore`, `quantita`, `data_inserimento`, `urgente` from ordini WHERE stato = 'merce_ordinata' order by fornitore ASC"
conn = New MySqlConnection(cnString)
Try
    conn.Open()
    da = New MySqlDataAdapter(sqlQRY, conn)
    Dim cb As MySqlCommandBuilder = New MySqlCommandBuilder(da)
    da.Fill(ds, "ok")
    form_cronologia_acquisti.dgv_cronologia_acquisti.DataSource = ds
    form_cronologia_acquisti.dgv_cronologia_acquisti.DataMember = "ok"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(0).HeaderText = ""
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(0).Width = 1
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(1).HeaderText = "Fornitore"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(1).Width = 100
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(2).HeaderText = "Articolo"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(2).Width = 300
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(3).HeaderText = "Cliente"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(3).Width = 100
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(4).HeaderText = "Scheda"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(4).Width = 50
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(5).HeaderText = "Colore"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(5).Width = 50
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(6).HeaderText = "Q.ta"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(6).Width = 50
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(7).HeaderText = "Data inserimento"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(7).Width = 130
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(8).HeaderText = "Urgente"
    form_cronologia_acquisti.dgv_cronologia_acquisti.Columns(8).Width = 1
Catch ex As Common.DbException
    MessageBox.Show("Errore: " & ex.Message, "Process", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
    conn.Close()
End Try
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
{count} votes

1 answer

Sort by: Most helpful
  1. KOZ6.0 6,395 Reputation points
    2023-09-25T16:07:27.2966667+00:00

    A simle sample

    Paste the DataGridView, BindingSource, and TextBox into the Form.

    Public Class Form1
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("Column1")
            dt.Columns.Add("Column2")
            dt.Columns.Add("Column3")
            dt.Rows.Add("AAAA", "AAAA", "AAAA")
            dt.Rows.Add("ABBB", "BBBB", "BBBB")
            dt.Rows.Add("ABCC", "CCCC", "CCCC")
            BindingSource1.DataSource = dt
            DataGridView1.DataSource = BindingSource1
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
                                                Handles TextBox1.TextChanged
            Dim value As String = TextBox1.Text
            Dim filter As String
            If String.IsNullOrWhiteSpace(value) Then
                filter = Nothing
            Else
                filter = $"Column1 LIKE '{value.Replace("'", "''").ToUpper()}*'"
            End If
            BindingSource1.Filter = filter
        End Sub
    
    End Class
    

    Let's put "abc" in the TextBox and see how it works.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.