C#编程-78:DataGridView隔行显示不同颜色

220 阅读1分钟

C#编程-78:DataGridView隔行显示不同颜色
\

\

\

\

  1. //方法一(推荐):

  2. //设置所有行背景色

  3. this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Violet;

  4. //设置奇数行背景色(下标从零开始)

  5. this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Blue;

  6.  

  7. //方法二:

  8. for (int i = 0; i < dataGridView1.Rows.Count; i++)

  9. {

  10.     if (i % 2 == 0) 

  11.         dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Pink;

  12.     else 

  13.         dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Violet;

  14. }