修改datagridView单元格颜色(行颜色,列颜色)

解决:将设置dataGridView1单元格背景颜色的代码放入RowPostPaint或RowPrePaint中,RowPostPaint在绘制dataGridView后发生,RowPrePaint在绘制dataGridView前发生
在这里插入图片描述

其中在 CellPainting 下才有 e.ColumnIndex 这样才能定位到单独表格

  private void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >=0)
            {
                DataGridViewRow dgr = DGV.Rows[e.RowIndex];
                ChangciInfo ThisInfo = dgr.DataBoundItem as ChangciInfo;
                if (e.ColumnIndex == 2)
                {
                    if (ThisInfo.HongId == ThisInfo.ShengFangId)
                    {
                        dgr.Cells[e.ColumnIndex].Style.BackColor = Color.LightSalmon;
                    }
                    else
                    {
                        dgr.Cells[e.ColumnIndex].Style.BackColor = Color.Red;
                    }
                }
                if (e.ColumnIndex == 3)
                {
                    if (ThisInfo.LanId == ThisInfo.ShengFangId)
                    {
                        dgr.Cells[e.ColumnIndex].Style.BackColor = Color.LightSalmon;
                    }
                    else
                    {
                        dgr.Cells[e.ColumnIndex].Style.BackColor = Color.Blue;
                    }
                }
            }
        }

按条件显示单元格底色
单元格底色

 dgr.Cells[e.ColumnIndex].Style.BackColor = Color.LightSalmon;
//包含Header全部的单元格的背景色为黄色
DataGridView1.DefaultCellStyle.BackColor = Color.Yellow;
 
//包含Header全部的单元格的前景色为黄色
DataGridView1.DefaultCellStyle.ForeColor= Color.Yellow; //前景色设置,只须要将BackColor改成ForeColor便可

// Header之外全部的单元格的背景色为黄色

DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow;

//(0, 0)单元格的背景色为粉色

DataGridView1[0, 0].Style.BackColor = Color.Pink;
//索引0列的单元格的背景色为淡蓝色
DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;
 
//索引0行的单元格的背景色为淡灰色
DataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;

变动奇数行的单元格Style
DataGridView.AlternatingRowsDefaultCellStyle属性,能够变动DataGridView的奇数行的单元格 Style。
以下面的例子,奇数行的单元格的背景色设定为黄绿色ci

1
2

//奇数行的单元格的背景色为黄绿色
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow;

6.变动列Header、行Header的单元格Style
列Header的单元格style的变动,能够使用,DataGridView.ColumnHeadersDefaultCellStyle属性实现。行 Header的单元格Style的变动,能够使用DataGridView.RowHeadersDefaultCellStyle属性实现。可是,Header 的是左侧的单元格须要经过DataGridView.TopLeftHeaderCell属性,取得的DataGridViewHeaderCell对象的单 元格Style进行设定。
以下面的例子,列Header的背景色为象牙色,行Header的背景色为橙色。table

//列Header的背景色为象牙色
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Ivory;

//行Header的背景色为橙色

DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Lime;

补充:每一个Header单元格的单元格Style,能够使用这一些的方法取得,和通常的单元格同样,能够使用Style 属性变动,简而言之,就是个能够对每一个单元格进行个性化设置。class

关于优先顺序方法

设定单元格Style的属性有优先顺序的。顺序从高到低以下所示。
1). DataGridViewCell.Style
2). DataGridViewRow.DefaultCellStyle
3). DataGridView.AlternatingRowsDefaultCellStyle
4). DataGridView.RowsDefaultCellStyle
5). DataGridViewColumn.DefaultCellStyle
6). DataGridView.DefaultCellStyle
接下来是Header的单元格Style属性的优先顺序。
1). DataGridViewCell.Style
2). DataGridView.RowHeadersDefaultCellStyle
3). DataGridView.ColumnHeadersDefaultCellStyle
4). DataGridView.DefaultCellStyle
单元格自己的设定的Style是最优先的。

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值