private void ColorizeDataGridView()
{
int numRows = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.None);
for (int i = 0; i < numRows; i++)
{
// Determine the alternating background color.
DataGridViewCellStyle dsg = new DataGridViewCellStyle(dataGridView1.Rows[i].DefaultCellStyle); // Get old style before modifying the background color.
if (i % 2 != 0)
{
dsg.BackColor = Color.FromArgb(255, 239, 239, 239); // Light Gray
}
else
{
dsg.BackColor = Color.FromArgb(255, 255, 255, 255); // White
}
// Apply the new background color and header text.
dataGridView1.Rows[i].DefaultCellStyle = dsg;
dataGridView1.Rows[i].HeaderCell.Value = Convert.ToString(i + 1);
}
}