Home > database >  Query remains in table when i clear the search box
Query remains in table when i clear the search box

Time:02-14

When I clear the search box in the form, the table shows the same info (I want the table to show the original one, without any query).

Code:

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

CodePudding user response:

If I've read the question correctly it appears that you're saying this:

  1. You add a search term to TextBox14 which is then applied to the CustomerInfo1BindingSource.Filter and in turn that filters CustomerInfo1DataGridView as expected
  2. You remove the search term from TextBox14 and click the button again
  3. The filtered CustomerInfo1DataGridView remains untouched rather than showing everything

If that is the case I can see that code flow isn't exactly what you're expecting. Change it to be something like:

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

As it stands with your code the lineCustomerInfoBindingSource.Filter = Nothing is not being hit because there is no search term in TextBox14. Instead it's calling this NotFound() method which we don't have visibility of, and then exiting the method.

It might be worth also reading up on the official documentation. You can call RemoveFilter on a BindingSource:

CustomerInfoBindingSource.RemoveFilter()
  • Related