如果该dataGridView是跟数据库绑定的,则可以触发DataBindingComplete事件:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1
privatevoiddataGridView1_DataBindingComplete(objectsender,DataGridViewBindingCompleteEventArgse)
2

{
3
if(this.dataGridView1.Rows.Count!=0)
4

{
5
for(inti=0;i<this.dataGridView1.Rows.Count;)
6

{
7
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor=System.Drawing.Color.Pink;
8
i+=2;
9
}
10
}
11
}
privatevoiddataGridView1_DataBindingComplete(objectsender,DataGridViewBindingCompleteEventArgse)2


{3
if(this.dataGridView1.Rows.Count!=0)4


{5
for(inti=0;i<this.dataGridView1.Rows.Count;)6


{7
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor=System.Drawing.Color.Pink;8
i+=2;9
}10
}11
}
如果没有绑定数据库,那么当dataGridView中的数据有所改变或显示的时候可以添加以下的代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1
if(this.dataGridView1.Rows.Count!=0)
2

{
3
for(inti=0;i<this.dataGridView1.Rows.Count;)
4

{
5
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor=System.Drawing.Color.Pink;
6
i+=2;
7
}
8
}
9
if(this.dataGridView1.Rows.Count!=0)2


{3
for(inti=0;i<this.dataGridView1.Rows.Count;)4


{5
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor=System.Drawing.Color.Pink;6
i+=2;7
}8
}9
轉自互邊網
本文介绍了如何通过编程方式为DataGridView中的交替行设置不同的背景颜色。提供了两种情况下的代码实现:一种是在DataGridView与数据库绑定时触发DataBindingComplete事件;另一种是在DataGridView未绑定数据库时直接进行操作。
2819

被折叠的 条评论
为什么被折叠?



