• 修改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;
                        }
                    }
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

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

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

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

    DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow;
    
    • 1

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

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

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

    1
    2

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

    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;
    
    • 1
    • 2

    //行Header的背景色为橙色

    DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Lime;
    
    • 1

    补充:每一个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是最优先的。

  • 相关阅读:
    Java 21 新特性:虚拟线程(Virtual Threads)
    CF1473C No More Inversions
    关于Ubuntu ssh远程连接报错和无法root登录的解决方法
    idea 创建java web项目 run后出现404现象
    Redis入门到通关之Redis网络模型-用户空间和内核态空间
    项目介绍:开源安防摄像机(嵌入式软件)
    Java操作文件的方法大全
    JavaScript面向对象:面向过程与面向对象
    String10:StringBuffer类结构剖析
    leetcode264 丑数 II
  • 原文地址:https://blog.csdn.net/weixin_40029679/article/details/126769496