vb.neteventsgridviewtextboxbindingsource

wants events in textbox with MessageBox and not gridview filter state if code is not found in vb.net


I want to appear MessageBox and not gridview filter state if code is not found.

Dim source1 As New BindingSource()
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox2.KeyDown
        If TextBox2.Text = "" Then
            source1.Filter = ""
            Me.DataGridView1.Refresh()
        Else
            If e.KeyCode = Keys.Enter Then
                source1.Filter = "CODE like  '' + '" & TextBox2.Text & "' + '' "
                Me.DataGridView1.Refresh()
            End If
        End If
    End Sub

view1 view2


Solution

  • Add another if statement inside the else then check if the source1 has rows, if rows <= 0 then clear the filter and the textbox.

    UPDATED

    Dim source1 As New BindingSource()
    Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox2.KeyDown
          If TextBox2.Text = "" Then
              source1.Filter = ""
              Me.DataGridView1.Refresh()
          Else
              If e.KeyCode = Keys.Enter Then
                  source1.Filter = "CODE like  '' + '" & TextBox2.Text & "' + '' "
               
                   ' If Else Statement to determine the count of your bindingsource
                   ' Check the results, if result is less than or equal to 0 
                   ' then prompt messagebox and clear the source filter
                   If source1.count <= 0
                       source1.Filter = ""
                       TextBox2.Clear()
                       MsgBox("No Result Found!", MsgBoxStyle.Exclamation)
                   End If
                   Me.DataGridView1.Refresh()
              End If
          End If
    End Sub