当我清除搜索框时,查询保留在 table

Query remains in table when i clear the search box

当我清除表单中的搜索框时,table 显示相同的信息(我希望 table 显示原始信息,无需任何查询)。

代码:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    If TextBox14.Text = "" Then
        Call NotFound()
        Exit Sub
    Else
        CustomerInfo1BindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & TextBox14.Text & "')" &
            "OR (CustomerName LIKE '" & TextBox14.Text & "') OR (CustomerNumber LIKE '" & TextBox14.Text & "')" &
            "OR (OrderDate LIKE '" & TextBox14.Text & "')"
        If CustomerInfo1BindingSource.Count <> 0 Then
            With CustomerInfo1DataGridView
                .DataSource = CustomerInfo1BindingSource
            End With

        Else
            MsgBox("Not Found!")
            CustomerInfoBindingSource.Filter = Nothing
        End If

    End If
End Sub

如果我没看错问题,你似乎是这样说的:

  1. 您将搜索词添加到 TextBox14,然后将其应用于 CustomerInfo1BindingSource.Filter,然后按预期过滤 CustomerInfo1DataGridView
  2. 您从 TextBox14 中删除了搜索词并再次单击该按钮
  3. 过滤后的 CustomerInfo1DataGridView 保持不变,而不是显示所有内容

如果是这种情况,我可以看出代码流并不完全符合您的预期。将其更改为:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    If TextBox14.Text = "" Then
        CustomerInfoBindingSource.Filter = Nothing
        MsgBox("Not Found!")
    Else
        CustomerInfo1BindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & TextBox14.Text & "')" &
            "OR (CustomerName LIKE '" & TextBox14.Text & "') OR (CustomerNumber LIKE '" & TextBox14.Text & "')" &
            "OR (OrderDate LIKE '" & TextBox14.Text & "')"
        If CustomerInfo1BindingSource.Count <> 0 Then
            With CustomerInfo1DataGridView
                .DataSource = CustomerInfo1BindingSource
            End With
        End If
    End If
End Sub

与您的代码一样,第 CustomerInfoBindingSource.Filter = Nothing 行未被命中,因为 TextBox14 中没有搜索词。相反,它会调用我们不可见的 NotFound() 方法,然后退出该方法。

可能还值得阅读官方文档。您可以在 BindingSource:

上调用 RemoveFilter
CustomerInfoBindingSource.RemoveFilter()