DataGridView控件隔行换色
原理就是先判断所在的行是偶数行还是奇数行,然后设置当前行的当前样式为需要的就可以了。
SqlConnection conn = new SqlConnection(@"数据库连接字符串");
SqlDataAdapter sda = new SqlDataAdapter("select * from CarColor", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Index % 2 == 0)
{
dataGridView1.Rows[dgvr.Index].DefaultCellStyle.BackColor = Color.WhiteSmoke;
}
else
{
dataGridView1.Rows[dgvr.Index].DefaultCellStyle.BackColor = Color.LightCyan;
}
}