使用文本框过滤Datagridview中的搜索

问题描述 投票:0回答:1

我有一个searchbox,如下图所示。我希望当我输入一个字母或数字时,数据gridview将显示与我在searchbox中输入的数据相匹配的数据。我已经有了一个代码,但它只适用于一列,它只能在最初工作,但是当我添加其他数据时,它根本不起作用。你能为此建议一个更好的代码吗?谢谢! :)

顺便说一句,这是我的过滤器代码:

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

vb.net-2010 ms-access-2013
1个回答
1
投票

试试这个,它应该在你尝试用另一个关键字搜索时摆脱错误,在这里我填写Employee_IDEmployee_Fname列,只是为了告诉你它是如何工作的:

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

现在,如果您需要搜索更多列,只需复制并粘贴上面的过滤代码并更改列的名称。

最后,我希望这会对你有所帮助,如果有的话请告诉我:)

© www.soinside.com 2019 - 2024. All rights reserved.