如果在 vb.net 中找不到代码,则希望事件在带有 MessageBox 的文本框中而不是 gridview 过滤器状态

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

如果找不到代码,我想显示 MessageBox 而不是 gridview 过滤器状态。

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

在 else 中添加另一个 if statement 然后检查 source1 是否有行,如果行 <= 0 则清除过滤器和文本框。

已更新

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