当我清除搜索框时,查询仍保留在表中



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

代码:

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

最新更新