How to do like this , each row has brown and white color in gridview c#.
foreach(DataGridview row in datagridview1.Rows
{
row.DefaultCellStyle.BackColor = Color.Brown;
row.DefaultCellStyle.BackColor = Color.White;
}
it takes only the last color . I need this to be like i showed you in image. I don’t want to use any third party library which is not free.
enter image description here
0
You set the color and then change it after the next line. That means for each row you set one color and the other just after.
Your loop needs to set the color depending on odd or even rows.
Use something like this
for (int i = 0; i <= datagridview1.Rows.Count; i++)
{
datagridview1.Rows[i].BackColor = i%%2 == 0 ? Color.Brown : Color.White;
}
This way, you say : if my row is even, the color is brown, else it is white.