FrustratedProgrammer
FrustratedProgrammer

Reputation: 21

Filter Search in Datagridview using textbox

I have a searchbox as shown in the image below. I want that when i type a letter or a number, the data gridview will display the data that matches to what I've inputted in the searchbox. I have already a code for that but it is only for one column and it only works at first but when I add other data, it is not working at all. Can you please suggest a better code for that? Thank you! :)

Btw, here's my code for the filter:

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
            EmployeeProfileBindingSource.Filter = String.Format("[Employee_Lname] Like '{0}%'",
  Me.txtSearch.Text.Trim())

        End Sub

enter image description here

Image here

Upvotes: 0

Views: 13304

Answers (1)

Mousa Alfhaily
Mousa Alfhaily

Reputation: 1290

Try this, it should get rid of the error when you try to search with another keyword, here I filled the Employee_ID and the Employee_Fname columns, just to show you how it works :

Public Class Form1
    Private dtTableGrd As DataTable
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = EmployeeProfileBindingSource 
        dtTableGrd = EmployeeProfileBindingSource
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        dtTableGrd.DefaultView.RowFilter = "Employee_Fname Like '%" & TextBox1.Text & "%'"
    End Sub
End Class

Now if you need to search in more columns, just copy and paste the filtering code above and change the name of the column.

Finally, I hope that will help you, please let me know if it does :)

Upvotes: 1

Related Questions