How to do the color for a particular row in a gridview?

I am using two forms,when I am sending the StockID from one form to another form,in the second form there is some data in the gridview,now I wan to do tat is,by using that id I want to highlight the that row only. I want to compare the ID is exists in the gridview or not,if exits the I want to Do the higlighten for that full row.,



if (pc == c)

{

    for (int i = 0; i < grid_stock.Rows.Count; i++)

    {       

grid_stock.SelectionMode = DataGridViewSelectionMode.FullRowSelect;       

this.grid_stock.Rows[i].DefaultCellStyle.BackColor = Color.Red;

        break;

    }                      

}

Last updated:3/10/2015 6:45:21 AM

1 Answers

Samuel Fernandes
Samuel Fernandes

Try this,

Give correct column index of StockID in row.Cells[0]

foreach (DataGridViewRow row in grid_stock.Rows) 
{
  if (row.Cells[0].Value.Equals('StockID'))
  {
    grid_stock.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    row.DefaultCellStyle.BackColor = Color.Red;
  }
}

Answer