שתף באמצעות


How to filter an unbound datagridview in vb.net?

Question

Sunday, November 2, 2014 3:19 AM

My datagridview is not bound to a database or a datatable. The contents of an array are loaded into it. Once the data is loaded, can I filter the data, for example by displaying only rows that have "1" in the first column?

Robert Homes

All replies (2)

Sunday, November 2, 2014 3:56 AM ✅Answered

I think I just answered my own question with this routine:

    Private Sub cboWeek_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboWeek.SelectedIndexChanged

        If cboWeek.Text = "all" Then
            For i = 0 To dg.RowCount - 1
                dg.Rows(i).Visible = True
            Next
        Else
            Dim n = cboWeek.Text

            For i = 0 To dg.RowCount - 1
                dg.Rows(i).Visible = (dg.Rows(i).Cells(0).Value = n)
            Next
        End If
    End Sub

"dg" is the name of the DataGridView

Robert Homes


Sunday, November 2, 2014 4:18 AM ✅Answered

I prefer using the DataTable as the source of the DGV. 

        Dim dv As DataView = DirectCast(DataGridView1.DataSource, DataTable).DefaultView
        dv.RowFilter = "FromColumn like '%" + textBox1.Text + "%'"
        DataGridView1.DataSource = dv

See webpages below for more info

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx

jdweng