C# winform datagridview 如何设置Row的颜色

1、隔行变色

设置:只要设置如下属性,就行了。

AlternatingRowsDefaultCellStyle 属性
获取或设置应用于 DataGridView 的奇数行的默认单元格样式。

RowsDefaultCellStyle 属性 
获取或设置应用于 DataGridView 的行单元格的默认样式。

只需要增加以下代码即可实现隔行变色
dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
效果图如下:

2、C#根据条件设置datagridview行的颜色

需要再 控件的 CellPainting (绘制单元格时发生的)事件

	private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)  
        {  
            if (e.RowIndex > -1)  
            {  
                int intGrade = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["Status"].Value);  
                if (intGrade ==30)  
                {  
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;  
                }  
                else if(intGrade==25)  
                {  
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Brown;  
                }  
            }  
        }  

3、C#中关于DataGridView行和列的背景色-前景色设置

请看这个篇文章

https://blog.csdn.net/wangzhen209/article/details/51744518

猜你喜欢

转载自blog.csdn.net/hht006158/article/details/79789639
今日推荐