Vb.net – Search Datagridview Vb.net

datagridviewsearchvb.net

I have been stuck in creating search for Datagridview in Vb.net.

I have one DataGridView that is bound to binding source. It contains data such as:

123456,
213926,
285643,
395687,

I have searched but everywhere but I only found a filter method for binding source or a find method.
The filter method removes remaining rows & find method finds an exact string.

I found a method to find text in DataGridView but that search found the string anywhere in DataGridView column like if user type 2 then it will first select the row having 2 such as 123456.

I want to create search that should find letter in sequence from start & so on.
If the user presses 2 then search should go for cell starting with 2 such as 213926.

Best Answer

Matter solved Frank. Thankyou So Much. Here is my complete code for others too.

Dim Found As Boolean = False
Dim StringToSearch As String = ""
Dim ValueToSearchFor As String = Me.TextBox1.Text.Trim.ToLower
Dim CurrentRowIndex As Integer = 0
Try
  If dgvDishonourReceipts.Rows.Count = 0 Then
         CurrentRowIndex = 0
  Else
         CurrentRowIndex = dgvDishonourReceipts.CurrentRow.Index + 1
  End If
  If CurrentRowIndex > dgvDishonourReceipts.Rows.Count Then
         CurrentRowIndex = dgvDishonourReceipts.Rows.Count - 1
  End If
  If dgvDishonourReceipts.Rows.Count > 0 Then
       For Each gRow As DataGridViewRow In dgvDishonourReceipts.Rows
          StringToSearch = gRow.Cells(4).Value.ToString.Trim.ToLower
          If InStr(1, StringToSearch, LCase(Trim(TextBox1.Text)), vbTextCompare) = 1 Then
            Dim myCurrentCell As DataGridViewCell = gRow.Cells(4)
            Dim myCurrentPosition As DataGridViewCell = gRow.Cells(0)
            dgvDishonourReceipts.CurrentCell = myCurrentCell
            CurrentRowIndex = dgvDishonourReceipts.CurrentRow.Index
            Found = True
          End If
          If Found Then Exit For
        Next
  End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try