本文介绍了为什么我的 DataGridView 列没有按照应有的方式着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

基于

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

更新

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

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

...但它只为一个单元格着色,而不是所有单元格,正如它的注释(//为所有单元格设置选择背景颜色.")声称的那样.

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

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

解决方案

→ 设置这些属性:

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,DataGridView 的.所有其他样式都继承自此(请参阅重要说明).

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

DefaultCellStyle 可以使用其他可以重新定义一些或所有常规属性的属性来覆盖:

另见:Windows 窗体 DataGridView 控件中的单元格样式

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

要在使用 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(更具体).

这里有更多信息:

Based on this, I added the following code, in an attempt to make the base background color of my cells/rows yellow and the alternating ones lightblue:

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

What happens in reality is that only the alternating/every other columns are affected, and I'm not so sure that the color is really light blue:

Why is the background color remaining the default white for half of the rows?

UPDATE

I revisited that microsoft page, and tried the following code:

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

...but it only colorizes one cell, not all, as its comment ("// Set the selection background color for all the cells.") claims it will do.

Here is how it looks now, with the above code:

Adding the third/final line made no difference either way, although it has the comment "// Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default// value won't override DataGridView.DefaultCellStyle.SelectionBackColor."

解决方案

→ Settings these properties:

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

you're defining the background Color of the DataGridView, which is the background Color of the Control itself, not the background Color of its Cells (and the background Color of alternate Rows).

→ With these other properties:

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

SelectionBackColor and SelectionForeColor, set the DataGridViewCellStyle Color of a Cells when selected, not in their normal state, which is determined by the general property, DefaultCellStyle, of the DataGridView. All other styles inherit from this (see the Important Note).

Designer Note: Setting the DefaultCellStyle - in the PropertyGrid - to non default values, because of inheritance and a glitch in the Designer of the DataGridViewCellStyle class, may not generate the expected result, both at design-time and run-time: the ForeColor and/or BackColor values are ignored and possibly reset to default. WrapMode and Alignment are reset to default values. If that's the case, set the RowTemplate.DefaultCellStyle instead.

The DefaultCellStyle can be overridden using other properties that can redefine some or all the general properties:

See also: Cell Styles in the Windows Forms DataGridView Control

IMPORTANT NOTE:
The more specific a style is, the more it can compromise the DataGridView performance when presenting a large number of Row (large is undefined, it means that when the grid is scrolled, you notice some form of glitches in the graphics and some delay). Since the general DefaultCellStyle is used to determine the presentation of a Cell, setting Styles for each Row or Cell causes the Control to read and set the Style for each Row or Cell that redefines the default style.
This activity can of course complicate the presentation. When coupled with the automatic Column sizing that measures all Cells, the performance takes a bad hit.

To set the default BackColor of Rows when AlternatingRowsDefaultCellStyle is used, you can either set the DataGridView.DefaultCellStyle or the DataGridView.RowsDefaultCellStyle.
Which one to choose depends on the design of the DataGridView.

You can use DefaultCellStyle if you need to specify this property to define the general aspect of the grid:

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

or RowsDefaultCellStyle, if the DefaultCellStyle basic properties (ForeColor, BackColor, Font etc.) are inherited from the DataGridView's Parent and you need to override this behavior per-Row, since the AlternatingRowsDefaultCellStyle override has already been specified.

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

Thus, if setting the DefaultCellStyle can work in your case, use this instead of RowsDefaultCellStyle (which is more specific).

Some more information here:

这篇关于为什么我的 DataGridView 列没有按照应有的方式着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:58