I have datagridview and now I would like to change background color of its each row depending whether row number is even or odd.
I thought that there must be easier way to reach that. Then using for example this part of code and modify it so it would change the colours of dtg's row. If this snippet of code is one of the ways to do that, may someone help me to improve it so it wouldn't throw exception when index is out if rabge?
public void bg_dtg()
{
try
{
for (int i = 0; i <= dataGridView1.Rows.Count ; i++)
{
if (IsOdd(i))
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
}
}
}
catch (Exception ex)
{
MessageBox.Show(""+ex);
}
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
Thank you for your time and answers.
There is a DataGridView
alternate row view style option in the forms designer. AlternatingRowsDefaultCellStyle
in the properties grid
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With