简体   繁体   中英

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

在此处输入图片说明

Image here

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM