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;
To change the color of a cell use the following snippet
dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Green;
The following code adds rows and columns to a DataGridView.
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[2].Name = "City";
dataGridView1.Rows.Add("Kate", "New York");
dataGridView1.Rows.Add("John", "Seattle");