c# datagridview – Find a row in DataGridView by specific cell value

Updated on     Kisan Patel

Problem:

Find a row in dataGridView based on column and value
Datagridview full row selection but get single cell value
Ho do I get the cell value from a datagridview using row index and column index in c#?
How can i get DataGridView row values and store in variables?

Solution:

This will give you the gridview row index for the value:

String searchValue = "somestring";
int rowIndex = -1;
foreach(DataGridViewRow row in DataGridView1.Rows)
{
    if(row.Cells[1].Value.ToString().Equals(searchValue))
    {
        rowIndex = row.Index;
        break;
    }
}

Or a LINQ query

int rowIndex = -1;

DataGridViewRow row = dgv.Rows
     .Cast()
     .Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue))
     .First();

rowIndex = row.Index;

then you can do:

dataGridView1.Rows[rowIndex].Selected = true;

C#