+92 332 4229 857 99ProjectIdeas@Gmail.com

Searching In DataGridView Through LINQ (C#.net)

Searching In DataGridView Through LINQ (C#.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.

using System.Linq;

IEnumerable<DataGridViewRow> rows =
         from DataGridViewRow row 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
         foreach (DataGridViewRow r in rows)
            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.

using System.Linq;

DataGridViewRow row2 =
        (from DataGridViewRow r in dgvRecord.Rows
         where r.Cells["PersonName"].Value.ToString().Equals(textBox1.Text)
         select r).FirstOrDefault();

         if (row2 != null)
            row2.Selected = true;