【问题标题】:Why are my DataGridView columns not colorizing as they should?为什么我的 DataGridView 列没有按应有的颜色着色?
【发布时间】:2021-08-20 02:33:06
【问题描述】:

基于this,我添加了以下代码,试图使我的单元格/行的基本背景颜色为黄色,而交替的背景颜色为浅蓝色:

dataGridView1.BackgroundColor = System.Drawing.Color.Yellow;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.LightBlue;

实际发生的情况是只有交替/每隔一列受到影响,我不太确定颜色是否真的是浅蓝色:

为什么一半行的背景颜色保持默认白色?

更新

我重新访问了那个微软页面,并尝试了以下代码:

dataGridView1.DefaultCellStyle.SelectionBackColor = 
    System.Drawing.Color.LightYellow;
dataGridView1.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Black;
dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = 
    System.Drawing.Color.Empty;

...但它只着色一个单元格,而不是所有单元格,因为它的注释(“// 为所有单元格设置选择背景颜色。”)声称它会这样做。

这是现在的样子,上面的代码:

添加第三/最后一行没有任何区别,尽管它有注释 "// 设置 RowHeadersDefaultCellStyle.SelectionBackColor 使其默认 // 值不会覆盖 DataGridView.DefaultCellStyle.SelectionBackColor。"

【问题讨论】:

    标签: c# winforms .net-core datagridview


    【解决方案1】:

    为什么一半的背景颜色保持默认白色 行?

    → 设置这些属性:

    dataGridView1.BackgroundColor = System.Drawing.Color.Yellow;
    dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.LightBlue;
    

    您正在定义 DataGridView 的背景颜色,它是控件本身的背景颜色,而不是其单元格的背景颜色(以及备用行的背景颜色)。

    → 使用这些其他属性:

    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.LightYellow;
    dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;
    dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty;
    

    SelectionBackColorSelectionForeColor,选择DataGridViewCellStyle选择单元的颜色,而不是在其正常状态下,由常规属性,DefaultCellStyle确定。所有其他样式都继承自此(请参阅重要说明)。

    设计师注意:将 PropertyGrid 中的 DefaultCellStyle 设置为非默认值,因为 DataGridViewCellStyle 类的设计器中的继承和故障,可能不会生成预期的结果,在设计时和运行时:ForeColor 和/或 BackColor 值被忽略并可能重置为默认值。 WrapMode 和 Alignment 被重置为默认值。如果是这种情况,请改为设置 RowTemplate.DefaultCellStyle

    DefaultCellStyle 可以使用其他可以重新定义部分或全部通用属性的属性来覆盖:

    另见:Cell Styles in the Windows Forms DataGridView Control

    重要提示
    样式越具体,在呈现大量 Row 时就越会损害 DataGridView 的性能(large 未定义,这意味着当网格滚动时,您会注意到某种形式的 图形中的故障和一些延迟)。由于一般DefaultCellStyle 用于确定单元格的呈现方式,因此为每个行或单元格设置样式会导致控件读取并设置重新定义默认样式的每个行或单元格的样式。
    这个活动当然会使演示复杂化。当与测量所有单元格的自动列大小相结合时,性能会受到严重影响。

    要在使用 AlternatingRowsDefaultCellStyle 时设置行的默认背景色,您可以设置DataGridView.DefaultCellStyleDataGridView.RowsDefaultCellStyle
    选择哪一个取决于 DataGridView 的设计。

    如果您需要指定此属性来定义网格的一般方面,您可以使用DefaultCellStyle

    [DataGridView].DefaultCellStyle.BackColor = Color.Yellow;
    

    RowsDefaultCellStyle,如果DefaultCellStyle 基本属性(ForeColor、BackColor、Font 等)是从 DataGridView 的 Parent 继承的,并且您需要逐行覆盖此行为,因为 AlternatingRowsDefaultCellStyle 覆盖已经被指定。

    [DataGridView].RowsDefaultCellStyle.BackColor = Color.Yellow;
    

    因此,如果设置 DefaultCellStyle 可以在您的情况下工作,请使用它而不是 RowsDefaultCellStyle(更具体)。

    这里有更多信息

    【讨论】:

      【解决方案2】:

      使用DataGridViewRow.RowDefaultStyle 设置默认行颜色。该链接中的示例详细显示了如何执行此操作。

      DataGridViewRow.BackColor 应用于未填充行的区域。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-25
        • 1970-01-01
        相关资源
        最近更新 更多