DataGridView隔行显示不同的颜色

本文介绍了如何通过编程方式为DataGridView中的交替行设置不同的背景颜色。提供了两种情况下的代码实现:一种是在DataGridView与数据库绑定时触发DataBindingComplete事件;另一种是在DataGridView未绑定数据库时直接进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果该dataGridView是跟数据库绑定的,则可以触发DataBindingComplete事件:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 private void dataGridView1_DataBindingComplete( object sender,DataGridViewBindingCompleteEventArgse)
2 {
3if(this.dataGridView1.Rows.Count!=0)
4{
5for(inti=0;i<this.dataGridView1.Rows.Count;)
6{
7this.dataGridView1.Rows[i].DefaultCellStyle.BackColor=System.Drawing.Color.Pink;
8i+=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 {
3for(inti=0;i<this.dataGridView1.Rows.Count;)
4{
5this.dataGridView1.Rows[i].DefaultCellStyle.BackColor=System.Drawing.Color.Pink;
6i+=2;
7}

8}

9
轉自互邊網
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值