DataGridView by Mailo

Find a Row in DataGridView Based on Value

To get the DataGridViewRow index use the following snippet

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

or use a Linq query

int rowIndex = -1;

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

rowIndex = row.Index;

Comments