Search code examples
datagridviewrowbackcolor

Datagridview row color which contents color in C#


i have a question about datagridview row color. there are records on my DGV fetching data from sql. i have a column like answer and it keeps data like Yes or No. i wish in DGV the columns value if yes: the row bg color is red or the coloumns value if no; the row bg color is blue. is it possible?


Solution

  • Try like this

     for (int i = 0; i < dataGridView1.Rows.Count;i++ )
     {
    
        if ((string)dataGridView1.Rows[i].Cells[0].Value == "Yes")
        {
            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
        }
        else
        {
            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
        }
    
    }