LRoyster LRoyster - 1 month ago 25
Vb.net Question

VB.Net 2015 Textbox to search DataGrid

I am wondering if there is an easy way to use a text box to search a datagrid. I have read some on it but all the examples I found were from older versions of VS. It seems the older versions of VS use a lot of code to connect to the data source so it uses a lot of code to search. I am thinking I can simplify this?

Private Sub Baseball_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.PlayersTableAdapter.Fill(Me.BaseballDataSet.Players)

End Sub


That loads the grid with the data. The table contains baseball players names and batting averages. I want to be able to use a textbox and a button to search by name and another to search by batting average.

Answer

This is from my sample code. I have one Textbox and one Button.

 Private Sub srchBtn_Click(sender As Object, e As EventArgs) Handles srchBtn.Click
    On Error GoTo wewe

    If txtSearch.Text = "" Then
        Call notFound()
        Exit Sub

    Else

        Dim cantFind As String = txtSearch.Text

        SampleBindingSource.Filter = "(Convert(Number_of_Employees, 'System.String') LIKE '" & txtSearch.Text & "') OR (ewan LIKE '" & txtSearch.Text & "') OR (ko LIKE '" & txtSearch.Text & "') OR (sayo LIKE '" & txtSearch.Text & "') OR (hehehe LIKE '" & txtSearch.Text & "')"

        If SampleBindingSource.Count <> 0 Then
            With DataGridView1
                .DataSource = SampleBindingSource
            End With
        Else

            MsgBox(cantFind & vbNewLine & "The search item was not found!", MsgBoxStyle.Information, "Hey boss")

            SampleBindingSource.Filter = Nothing

            With DataGridView1
                .ClearSelection()
                .DataSource = SampleBindingSource
            End With


        End If
    End If


hey:
    Exit Sub

wewe:

    MsgBox("Error Number " & Err.Number & vbNewLine & "Error Description " & Err.Description, MsgBoxStyle.Critical, "Reset Error!")
    Resume hey

End Sub

Private Sub reset()
    Dim txtS As TextBox = txtSearch

    With txtS
        .Text = ""
        .Select()

    End With

    If DataGridView1.DataSource Is Nothing Then
        Exit Sub
    End If

End Sub

Private Sub notFound()
    Dim txtS As TextBox = txtSearch

    With txtSearch
        .Text = ""
        .Select()
        .SelectAll()

    End With

    If DataGridView1.DataSource Is Nothing Then
        Exit Sub
    End If
End Sub

This will find a specific word by clicking the button. Note: Place that code in your Button. It will ONLY find a word that spelled correctly, or a whole word.

Example: You want to find the word Anthony. Input the whole word then click the search button.

If you want to search an item without clicking any button, you can try this:

  Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
    Try

        Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Yourdatabase.mdb;")
            conn.Open()
            Dim command As New OleDbCommand("SELECT * FROM yourtable WHERE (ID like @ID) OR (Sample like @Sample) OR (Sample like @Sample) OR (Sample like @Sample) OR (Sample like @Sample)", conn)
            With command.Parameters
                .AddWithValue("@Sample", TextBox5.Text)
                .AddWithValue("@Sample", TextBox5.Text)
                .AddWithValue("@Sample", TextBox5.Text)
                .AddWithValue("@Sample", TextBox5.Text)
                .AddWithValue("@Sample", TextBox5.Text)
            End With
            Dim adapter As New OleDbDataAdapter
            Dim dt As New DataTable
            adapter.SelectCommand = command5
            adapter.Fill(dt5)
            DataGridView.DataSource = dt
            adapter.Dispose()
            command.Dispose()
            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR4", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Place that code in the TextChanged event in Textbox. Note: I used a connection string.

Hope this helps.