+92 332 4229 857 99ProjectIdeas@Gmail.com

Searching In DataGridView Through LINQ (Vb.net)

The following code shows you searching in DataGridView through LINQ, you write a search word in textbox and if that name is listed in DataGridView it will select that row and if the word is listed more than one it would select all those rows.

Selecting multiple rows of searched word if found. 

Imports System.Linq

Dim query As IEnumerable(Of DataGridViewRow) = _
          From row As DataGridViewRow In dgvRecord.Rows _
          Where row.Cells("PersonName").Value.ToString().Equals(TextBox1.Text) _
          Select row

' where row.Cells("PersonName").Value.ToString().Contains(TextBox1.Text)
' You can also check like this
     For Each r As DataGridViewRow In query
          r.Selected = True

If that searched word is listed more than one in your DataGridView then selecting the very first row it found of that searched word.

Imports System.Linq

Dim row As DataGridViewRow = _
        (From r As DataGridViewRow In dgvRecord.Rows _
         Where r.Cells("PersonName").Value.ToString().Equals(TextBox1.Text) _
         Select r).FirstOrDefault()

   If Not row Is Nothing Then
        row.Selected = True
   End If