Vb.net DataGrid查看查找文本

时间:2014-06-11 04:39:39

标签: vb.net

如何通过快捷键(Shift + F)在dataGridView中查找用户搜索的字符串,以及Visual Studio 2005 Windows应用程序中DataGridView的快捷键处理程序事件是什么。

1 个答案:

答案 0 :(得分:0)

Public Class Form1

  Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim dtb As New DataTable
    dtb.Columns.Add(New DataColumn("Col1"))
    dtb.Columns.Add(New DataColumn("Col2"))
    dtb.Columns.Add(New DataColumn("Col3"))
    dtb.Columns.Add(New DataColumn("Col4"))
    dtb.Rows.Add("apple banana", "carrot date", "eggplant fig", "guava horseradish")
    dtb.Rows.Add("ant beetle", "cricket dung beetle", "earwig fly", "grasshopper horntail")
    DataGridView1.DataSource = dtb

  End Sub

  Private Sub DataGridView1_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyUp
    If e.KeyData = (Keys.Control Or Keys.F) Then
      Dim strSearch As String = InputBox("Enter search term", , "fig")

      For intRow As Integer = 0 To DataGridView1.Rows.Count - 1
        For intCol As Integer = 0 To DataGridView1.Columns.Count - 1
          If DataGridView1.Rows(intRow).Cells(intCol).Value.ToString.Contains(strSearch) Then
            DataGridView1.ClearSelection()
            DataGridView1.Rows(intRow).Cells(intCol).Selected = True
            Exit Sub
          End If
        Next intCol
      Next intRow

    End If
  End Sub
End Class