DataGridView行设置背景颜色+Var类型+筛选

本文介绍了如何在DataGridView中设置行的背景颜色,包括使用RowPrePaint事件、使用Var类型以及实现不同方式的颜色设定,如索引、隔行和筛选条件下的颜色变化。同时,文章还探讨了Var类型的使用,强调其能根据上下文推断类型,提高效率。

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

关键字

DefaultCellStyle

常用事件

RowPrePaint()
RowPrePaint()方法在发生任何单元格之前,行绘制时引发事件。
在自动添加新行时,背景颜色也会跟着绘制,在这个事件中。


主要关键语句


dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;

var rowindex = dataGridView1.Rows[e.RowIndex];
rowindex.DefaultCellStyle.ForeColor = Color.White;
rowindex.DefaultCellStyle.BackColor = Color.Blue;

DataGridViewRow dgrRoe = dataGridView1.Rows[e.RowIndex];
dgrRoe.DefaultCellStyle.ForeColor = Color.White;//前景色
dgrRoe.DefaultCellStyle.BackColor = Color.Red;//背景色

②③都是先保存行,在设置背景颜色


Var

VAR可代替任何类型,编译器会根据上下文来判断你到底是想用什么类型,类似 OBJECT,但是效率比OBJECT高点。


前景色代表是字体
背景色代表是字体的背景

第一个设置颜色代码

            if (e.RowIndex >= dataGridView1.Rows.Count - 1)
                return;
            else
            {
                Color Back = new Color();
                Color Fore = new Color();

                var rowindex = dataGridView1.Rows[e.RowIndex];
                if (rowindex == dataGridView1.CurrentRow)
                {
                    if (rowindex.DefaultCellStyle.ForeColor != Color.White)
                    {
                        Fore = rowindex.DefaultCellStyle.ForeColor;
                        rowindex.DefaultCellStyle.ForeColor = Color.White;
                    }
                    if (rowindex.DefaultCellStyle.BackColor != Color.Blue)
                    {
                        Back = rowindex.DefaultCellStyle.BackColor;
                        rowindex.DefaultCellStyle.BackColor = Color.Blue;
                    }
                }
                else
                {
                    rowindex.DefaultCellStyle.ForeColor = Fore;
                    rowindex.DefaultCellStyle.BackColor = Back;
                }
            }

第二个设置颜色代码(与第一个设置方式类似,代码减少)

            DataGridViewRow dgrRoe = dataGridView1.Rows[e.RowIndex];
            if (e.RowIndex < dataGridView1.Rows.Count - 1)
            {
                if (dgrRoe == dataGridView1.CurrentRow)
                {
                    dgrRoe.DefaultCellStyle.ForeColor = Color.White;//前景色
                    dgrRoe.DefaultCellStyle.BackColor = Color.Red;//背景色
                }
                else
                {
                    dgrRoe.DefaultCellStyle.ForeColor = Color.SaddleBrown;
                    dgrRoe.DefaultCellStyle.BackColor = Color.Silver;
                }
            }
            else { return; }

第三个设置颜色代码(索引与整行)

            if (dataGridView1.Rows[e.RowIndex].Index >= dataGridView1.Rows.Count - 1)
            {
                return;
            }
            else
            {
                if (dataGridView1.Rows[e.RowIndex] == dataGridView1.CurrentRow)
                {
                    //以下两种方法可行
                    i++;
                    //Console.WriteLine("执行次数:"+i+"\n行的索引:"+e.RowIndex);//每行执行三次,行索引为"当前鼠标点击那一行的索引"。
                    //dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
                    //dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;

                    Console.WriteLine("i的执行次数:" + i + "\n行的索引:" + dataGridView1.CurrentRow);//每行执行三次,行索引为"当前鼠标点击那一行的索引"。
                    dataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.White;
                    dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.Red;
                }
                else
                {
                    Console.WriteLine("j的执行次数:" + j + "\n行的索引:" + e.RowIndex);
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.SandyBrown;
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Orange;
                }
            }

第四个设置颜色代码(隔行)

第一种隔行的显示方式:

 var datagrid = dataGridView1.Rows[0];
            for (int i = 0; i < dataGridView1.Rows.Count; i += 2)
            {
                //隔一行颜色设置
                if (i < dataGridView1.Rows.Count - 1)
                {
                    datagrid = dataGridView1.Rows[i];
                    datagrid.DefaultCellStyle.ForeColor = Color.White;
                    datagrid.DefaultCellStyle.BackColor = Color.Blue;
                }
            } 

第二种隔行的显示方式:

this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Pink;//行单元格默认样式
this.dataGridView1.AlternaingRowsDefaultCellStyle.BackColor = Color.Red;//奇数(单数)行的默认样式

第五个设置颜色代码(筛选)

筛选关键语句:
DataView dv = new DataView(dt,”gender = ‘女’”,”age Asc”,DataViewRowState.CurrentRows);

            if (dataGridView1.CurrentRow.Index <= dataGridView1.Rows.Count - 1)
            {
                DataGridViewRow dgvr = dataGridView1.Rows[0];
                for (int i = 0; i < dv.Count; i++)
                {
                    dgvr = dataGridView1.Rows[i];
                    dgvr.DefaultCellStyle.ForeColor = Color.White;
                    dgvr.DefaultCellStyle.BackColor = Color.Blue;
                }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值