Search datagridview on user keypress

前端 未结 5 1282
别跟我提以往
别跟我提以往 2021-02-09 05:09

I\'m trying to select the first row where the cell value starts with the same keychar the user pressed. That\'s the part that is giving me trouble.

Here\'s how I\'m hand

5条回答
  •  無奈伤痛
    2021-02-09 05:25

    This is a VS2008 VB.NET DataGridView extension meant to do kind of what you are doing but using a TextBox for searching information (not designed with case in mind but could easily be added). This extension works so perhaps there is something that might be helpful. I did notice that your code selects a row using select where mine uses CurrentCell.

         _
    Public Function PartSeek(ByVal GridView As DataGridView, ByVal ColumnName As String, ByVal Value As String, ByVal Part As Boolean) As Boolean
        Dim Located As Boolean = False
    
        If GridView.Columns.Contains(ColumnName) Then
            Dim SingleRow As DataGridViewRow
            If Part Then
                SingleRow = (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() _
                             Where Rows.Cells(ColumnName).Value.ToString().Contains(Value)).FirstOrDefault
            Else
                SingleRow = (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() _
                             Where Rows.Cells(ColumnName).Value.ToString() = Value).FirstOrDefault
            End If
            If Not IsNothing(SingleRow) Then
                If GridView.CurrentCell.RowIndex <> SingleRow.Index Then
                    GridView.CurrentCell = GridView(0, SingleRow.Index)
                End If
                DirectCast(GridView.Parent, Form).ActiveControl = GridView
                Located = True
            End If
            Return Located
        Else
            Throw New Exception("Column '" & ColumnName & "' not contained in this DataGridView")
        End If
    
    End Function
    

提交回复
热议问题