Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Filtersearching a Data Grid View

2010-03-09

Some applications have that cool search feature that filters on what you search for, and displays the result in real time. The data that you are searching in is loaded, and then the record(s) that matches your search from that set of data is display. Usually, you would do this if you are handling fairly little data, and you would do this on a separate thread from the GUI thread, to prevent that your application get poor responsiveness. This whole process works incredible well with the Data Grid View of the .NET Framework.

The following example requires a form with a textbox (TextBox1) and a data grid view (DataGridView1). To use the application, just type something in the textbox, and watch the grid filtering its records. The Load event of the form populates the grid. The TextChanged of the textbox filters the grid.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Configure the data grid view.
    DataGridView1.ReadOnly = True
    DataGridView1.Columns.Add("ColA", "Some column")
    DataGridView1.Columns.Add("ColB", "Some other column")
    DataGridView1.AllowUserToAddRows = False
    DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    DataGridView1.Rows.Clear()

    'Add lots of rows.
    For A As Integer = 500 To 999
        Dim Index As Integer = DataGridView1.Rows.Add()
        DataGridView1.Rows(Index).Cells(0).Value = A.ToString("000")
        DataGridView1.Rows(Index).Cells(1).Value = "Hello"
    Next

    'Add two rows with some names in them.
    Dim Temp As Integer = DataGridView1.Rows.Add()
    DataGridView1.Rows(Temp).Cells(0).Value = "Anders"
    DataGridView1.Rows(Temp).Cells(1).Value = "Bengt"

    Temp = DataGridView1.Rows.Add()
    DataGridView1.Rows(Temp).Cells(0).Value = "Calle"
    DataGridView1.Rows(Temp).Cells(1).Value = "David"

    'Again, add lots of rows.
    For A As Integer = 300 To 999
        Dim Index As Integer = DataGridView1.Rows.Add()
        DataGridView1.Rows(Index).Cells(0).Value = A.ToString("000")
        DataGridView1.Rows(Index).Cells(1).Value = "Good bye"
    Next

    'Now we have 1.202 rows in the grid. Do some resizing of the columns.
    DataGridView1.AutoResizeColumns()

End Sub

Private Sub TextBox1_TextChanged(ByVal _
     sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    If DataGridView1.RowCount > 0 Then

        'What is the user searching for? Remove case information.
        Dim SearchFor As String = TextBox1.Text.ToLower().Trim()

        'If the user has cleared the search box, show all rows.
        If SearchFor = "" Then
            For I As Integer = 0 To DataGridView1.Rows.Count - 1
                DataGridView1.Rows(I).Visible = True
            Next
        Else

            'When the user types something in the textbox, search for
            'rows with cells that holds that value.
            For I As Integer = 0 To DataGridView1.Rows.Count - 1

                'Extract the values from the grid. Remove case information.
                Dim ColA As String = CType(DataGridView1.Rows(I).Cells(0).Value, _
String).ToLower()
                Dim ColB As String = CType(DataGridView1.Rows(I).Cells(1).Value, _
String).ToLower()

                'Show only matching rows.
                If ColA.IndexOf(SearchFor) > -1 OrElse ColB.IndexOf(SearchFor) > -1 Then
                    DataGridView1.Rows(I).Visible = True
                Else
                    DataGridView1.Rows(I).Visible = False
                End If

            Next
        End If

        'Select the first visible row.
        For I As Integer = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(I).Visible Then
                DataGridView1.CurrentCell = DataGridView1.Rows(I).Cells(0)
                Exit For
            End If
        Next

    End If
End Sub

Categories: Visual Basic 9

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips