使用文本框

时间:2017-08-02 03:53:11

标签: vb.net-2010 ms-access-2013

我有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

enter image description here

Image here

1 个答案:

答案 0 :(得分:0)

试试这个,当你尝试使用其他关键字进行搜索时,它应该摆脱错误,在这里我填写了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

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

最后,我希望对您有所帮助,如果有,请告诉我。)