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

关键字

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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值