Windows 窗体 DataGridView 控件中的单元格样式Cell Styles in the Windows Forms DataGridView Control

控件中的DataGridView每个单元格都可以有自己的样式, 如文本格式、背景色、前景色和字体。Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. 不过, 通常情况下, 多个单元格将共享特定的样式特征。Typically, however, multiple cells will share particular style characteristics.

共享样式的单元格组可以包含特定行或列中的所有单元格、包含特定值的所有单元格或控件中的所有单元格。Groups of cells that share styles may include all cells within particular rows or columns, all cells that contain particular values, or all cells in the control. 由于这些组重叠, 因此每个单元格可能会从多个位置获取其样式信息。Because these groups overlap, each cell may get its styling information from more than one place. 例如, 您可能想要DataGridView控件中的每个单元格都使用同一字体, 但只有货币列中的单元格使用货币格式, 而只有包含负数的货币单元格才能使用红色前景色。For example, you may want every cell in a DataGridView control to use the same font, but only cells in currency columns to use currency format, and only currency cells with negative numbers to use a red foreground color.

DataGridViewCellStyle 类The DataGridViewCellStyle Class

DataGridViewCellStyle类包含以下与视觉样式相关的属性:The DataGridViewCellStyle class contains the following properties related to visual style:

此类还包含以下与格式设置相关的属性:This class also contains the following properties related to formatting:

有关这些属性以及其他单元格样式属性的详细信息, 请参阅DataGridViewCellStyle下面的 "另请参阅" 部分中列出的参考文档和主题。For more information on these properties and other cell-style properties, see the DataGridViewCellStyle reference documentation and the topics listed in the See Also section below.

使用 DataGridViewCellStyle 对象Using DataGridViewCellStyle Objects

可以从DataGridViewDataGridViewCellStyleDataGridViewColumn和类DataGridViewCell及其派生类的各种属性中检索对象。 DataGridViewRowYou can retrieve DataGridViewCellStyle objects from various properties of the DataGridView, DataGridViewColumn, DataGridViewRow, and DataGridViewCell classes and their derived classes. 如果尚未设置这些属性中的一个, 则检索其值会创建一个新DataGridViewCellStyle的对象。If one of these properties has not yet been set, retrieving its value will create a new DataGridViewCellStyle object. 你还可以实例化你DataGridViewCellStyle自己的对象并将其分配给这些属性。You can also instantiate your own DataGridViewCellStyle objects and assign them to these properties.

通过在多个DataGridViewCellStyle DataGridView元素之间共享对象, 可以避免不必要地重复样式信息。You can avoid unnecessary duplication of style information by sharing DataGridViewCellStyle objects among multiple DataGridView elements. 由于在控件、列和行级别设置的样式会向下筛选每个级别到单元格级别, 因此还可以通过仅设置每个级别上不同于上述级别的样式属性来避免样式重复。Because the styles set at the control, column, and row levels filter down through each level to the cell level, you can also avoid style duplication by setting only those style properties at each level that differ from the levels above. 下面的 "样式继承" 部分对此进行了详细介绍。This is described in more detail in the Style Inheritance section that follows.

下表列出了获取或设置DataGridViewCellStyle对象的主要属性。The following table lists the primary properties that get or set DataGridViewCellStyle objects.

PropertyProperty Classes 描述Description
DefaultCellStyle DataGridViewDataGridViewColumnDataGridViewRow和派生类DataGridView, DataGridViewColumn, DataGridViewRow, and derived classes 获取或设置整个控件中的所有单元格 (包括标头单元格)、列或行中的默认样式。Gets or sets default styles used by all cells in the entire control (including header cells), in a column, or in a row.
RowsDefaultCellStyle DataGridView 获取或设置控件中的所有行使用的默认单元格样式。Gets or sets default cell styles used by all rows in the control. 这不包括标头单元格。This does not include header cells.
AlternatingRowsDefaultCellStyle DataGridView 获取或设置控件中的交替行使用的默认单元格样式。Gets or sets default cell styles used by alternating rows in the control. 用于创建类似帐目型的效果。Used to create a ledger-like effect.
RowHeadersDefaultCellStyle DataGridView 获取或设置控件的行标题使用的默认单元格样式。Gets or sets default cell styles used by the control's row headers. 如果启用了视觉样式, 则由当前主题重写。Overridden by the current theme if visual styles are enabled.
ColumnHeadersDefaultCellStyle DataGridView 获取或设置控件的列标题使用的默认单元格样式。Gets or sets default cell styles used by the control's column headers. 如果启用了视觉样式, 则由当前主题重写。Overridden by the current theme if visual styles are enabled.
Style DataGridViewCell和派生类DataGridViewCell and derived classes 获取或设置在单元格级别指定的样式。Gets or sets styles specified at the cell level. 这些样式会重写从更高级别继承的样式。These styles override those inherited from higher levels.
InheritedStyle DataGridViewCellDataGridViewRowDataGridViewColumn和派生类DataGridViewCell, DataGridViewRow, DataGridViewColumn, and derived classes 获取当前应用于单元格、行或列的所有样式, 包括从更高级别的继承的样式。Gets all the styles currently applied to the cell, row, or column, including styles inherited from higher levels.

如上所述, 如果以前尚未设置属性, 则获取样式属性的值会DataGridViewCellStyle自动实例化新的对象。As mentioned above, getting the value of a style property automatically instantiates a new DataGridViewCellStyle object if the property has not been previously set. 若要避免不必要地创建这些对象, 则行和列HasDefaultCellStyle类具有一个属性, 你可以对其DefaultCellStyle进行检查以确定是否已设置该属性。To avoid creating these objects unnecessarily, the row and column classes have a HasDefaultCellStyle property that you can check to determine whether the DefaultCellStyle property has been set. 同样, cell 类具有一个HasStyle属性, 该属性指示Style是否已设置属性。Similarly, the cell classes have a HasStyle property that indicates whether the Style property has been set.

每个样式属性在DataGridView控件上都有一个对应的PropertyName Changed事件。Each of the style properties has a corresponding PropertyNameChanged event on the DataGridView control. 对于行、列和单元属性, 事件Row的名称以 ""、"Column" 或 "Cell" (例如RowDefaultCellStyleChanged) 开头。For row, column, and cell properties, the name of the event begins with "Row", "Column", or "Cell" (for example, RowDefaultCellStyleChanged). 当相应的样式属性设置为另DataGridViewCellStyle一对象时, 将发生这些事件中的每一个。Each of these events occurs when the corresponding style property is set to a different DataGridViewCellStyle object. 从样式属性检索DataGridViewCellStyle对象并修改其属性值时, 不会发生这些事件。These events do not occur when you retrieve a DataGridViewCellStyle object from a style property and modify its property values. 若要对单元格样式对象本身的更改做出响应, CellStyleContentChanged请处理事件。To respond to changes to the cell style objects themselves, handle the CellStyleContentChanged event.

样式继承Style Inheritance

DataGridViewCell个从其InheritedStyle属性中获取其外观。Each DataGridViewCell gets its appearance from its InheritedStyle property. DataGridViewCellStyle属性返回的对象从类型DataGridViewCellStyle为的属性的层次结构继承其值。The DataGridViewCellStyle object returned by this property inherits its values from a hierarchy of properties of type DataGridViewCellStyle. 下面列出了这些属性, 这些属性的顺序如下InheritedStyle : 非标头单元格获取其值的顺序。These properties are listed below in the order in which the InheritedStyle for non-header cells obtains its values.

  1. DataGridViewCell.Style

  2. DataGridViewRow.DefaultCellStyle

  3. DataGridView.AlternatingRowsDefaultCellStyle(仅适用于具有奇数索引号的行中的单元格)DataGridView.AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)

  4. DataGridView.RowsDefaultCellStyle

  5. DataGridViewColumn.DefaultCellStyle

  6. DataGridView.DefaultCellStyle

对于行标题单元格和列标题InheritedStyle单元格, 按给定顺序按下面的源属性列表中的值填充属性。For row and column header cells, the InheritedStyle property is populated by values from the following list of source properties in the given order.

  1. DataGridViewCell.Style

  2. DataGridView.ColumnHeadersDefaultCellStyleDataGridView.RowHeadersDefaultCellStyleDataGridView.ColumnHeadersDefaultCellStyle or DataGridView.RowHeadersDefaultCellStyle

  3. DataGridView.DefaultCellStyle

下图演示了此过程。The following diagram illustrates this process.

DataGridViewCellStyle 类型的属性Properties of type DataGridViewCellStyle

还可以访问特定行和列继承的样式。You can also access the styles inherited by specific rows and columns. InheritedStyle属性从以下属性继承其值。The column InheritedStyle property inherits its values from the following properties.

  1. DataGridViewColumn.DefaultCellStyle

  2. DataGridView.DefaultCellStyle

Row InheritedStyle属性从以下属性继承其值。The row InheritedStyle property inherits its values from the following properties.

  1. DataGridViewRow.DefaultCellStyle

  2. DataGridView.AlternatingRowsDefaultCellStyle(仅适用于具有奇数索引号的行中的单元格)DataGridView.AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)

  3. DataGridView.RowsDefaultCellStyle

  4. DataGridView.DefaultCellStyle

对于DataGridViewCellStyle InheritedStyle属性返回的对象中的每个属性, 将从相应列表中的第一个单元格样式获取属性值, 该列表中的相应属性设置为默认值以外DataGridViewCellStyle的值。For each property in a DataGridViewCellStyle object returned by an InheritedStyle property, the property value is obtained from the first cell style in the appropriate list that has the corresponding property set to a value other than the DataGridViewCellStyle class defaults.

下表说明了如何从ForeColor其包含列继承示例单元格的属性值。The following table illustrates how the ForeColor property value for an example cell is inherited from its containing column.

类型的属性DataGridViewCellStyleProperty of type DataGridViewCellStyle 检索ForeColor的对象的示例值Example ForeColor value for retrieved object
DataGridViewCell.Style Color.Empty
DataGridViewRow.DefaultCellStyle Color.Red
DataGridView.AlternatingRowsDefaultCellStyle Color.Empty
DataGridView.RowsDefaultCellStyle Color.Empty
DataGridViewColumn.DefaultCellStyle Color.DarkBlue
DataGridView.DefaultCellStyle Color.Black

在这种情况下Color.Red , 该单元格的行中的值是列表中的第一个实际值。In this case, the Color.Red value from the cell's row is the first real value on the list. 这将成为ForeColorInheritedStyle单元格的属性值。This becomes the ForeColor property value of the cell's InheritedStyle.

下图说明了不同DataGridViewCellStyle属性如何从不同的位置继承其值。The following diagram illustrates how different DataGridViewCellStyle properties can inherit their values from different places.

DataGridView 属性-值继承DataGridView property-value inheritance

利用样式继承, 可以为整个控件提供适当的样式, 而不必在多个位置指定相同的信息。By taking advantage of style inheritance, you can provide appropriate styles for the entire control without having to specify the same information in multiple places.

尽管标头单元格参与到样式继承 (如所述), ColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle DataGridView控件的和属性返回的对象具有初始属性值, 这些值将重写由返回的对象的属性值。DefaultCellStyle属性。Although header cells participate in style inheritance as described, the objects returned by the ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle properties of the DataGridView control have initial property values that override the property values of the object returned by the DefaultCellStyle property. 如果要将DefaultCellStyle属性返回的对象的属性设置为应用于行标题和列标题, 则必须将ColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle属性返回的对象的相应属性设置为指定的默认值DataGridViewCellStyle对于类, 为。If you want the properties set for the object returned by the DefaultCellStyle property to apply to row and column headers, you must set the corresponding properties of the objects returned by the ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle properties to the defaults indicated for the DataGridViewCellStyle class.

备注

如果启用了视觉样式, 则行标题和列标题 (除外TopLeftHeaderCell) 将自动按当前主题样式进行样式, 并覆盖这些属性指定的任何样式。If visual styles are enabled, the row and column headers (except for the TopLeftHeaderCell) are automatically styled by the current theme, overriding any styles specified by these properties.

、和类型还初始化由列DefaultCellStyle属性返回的对象的某些值。 DataGridViewCheckBoxColumn DataGridViewImageColumn DataGridViewButtonColumnThe DataGridViewButtonColumn, DataGridViewImageColumn, and DataGridViewCheckBoxColumn types also initialize some values of the object returned by the column DefaultCellStyle property. 有关详细信息, 请参阅这些类型的参考文档。For more information, see the reference documentation for these types.

动态设置样式Setting Styles Dynamically

若要自定义具有特定值的单元格样式, 请实现DataGridView.CellFormatting事件的处理程序。To customize the styles of cells with particular values, implement a handler for the DataGridView.CellFormatting event. 此事件的处理程序接收DataGridViewCellFormattingEventArgs类型的参数。Handlers for this event receive an argument of the DataGridViewCellFormattingEventArgs type. 此对象包含一些属性, 这些属性使你可以确定要设置格式的单元格的值及其DataGridView在控件中的位置。This object contains properties that let you determine the value of the cell being formatted along with its location in the DataGridView control. 此对象还包含一个CellStyle属性, 该属性被初始化为正在格式化InheritedStyle的单元格的属性值。This object also contains a CellStyle property that is initialized to the value of the InheritedStyle property of the cell being formatted. 您可以修改单元样式属性以指定适合单元值和位置的样式信息。You can modify the cell style properties to specify style information appropriate to the cell value and location.

备注

和事件也会接收DataGridViewCellStyle到事件数据中的对象, 但在这种情况下, 它是行InheritedStyle属性的副本以进行只读的, 并且不会影响控件。 RowPostPaint RowPrePaintThe RowPrePaint and RowPostPaint events also receive a DataGridViewCellStyle object in the event data, but in their case, it is a copy of the row InheritedStyle property for read-only purposes, and changes to it do not affect the control.

您还可以动态修改各个单元格的样式, 以响应DataGridView.CellMouseEnterCellMouseLeave事件之类的事件。You can also dynamically modify the styles of individual cells in response to events such as the DataGridView.CellMouseEnter and CellMouseLeave events. 例如, 在CellMouseEnter事件的处理程序中, 您可以存储单元格背景色的当前值 (通过单元格的Style属性检索), 然后将其设置为新的颜色, 当鼠标悬停在该单元格上时, 将突出显示该单元格。For example, in a handler for the CellMouseEnter event, you could store the current value of the cell background color (retrieved through the cell's Style property), then set it to a new color that will highlight the cell when the mouse hovers over it. CellMouseLeave事件的处理程序中, 您可以将背景色还原为原始值。In a handler for the CellMouseLeave event, you can then restore the background color to the original value.

备注

不管是否设置了某个特定样式值Style , 缓存存储在单元格中的值都非常重要。Caching the values stored in the cell's Style property is important regardless of whether a particular style value is set. 如果你暂时替换某个样式设置, 则将其还原到其原始 "未设置" 状态可确保单元格返回到从较高级别继承样式设置。If you temporarily replace a style setting, restoring it to its original "not set" state ensures that the cell will go back to inheriting the style setting from a higher level. 如果需要确定单元的实际有效样式, 而不管样式是否继承, 请使用单元格的InheritedStyle属性。If you need to determine the actual style in effect for a cell regardless of whether the style is inherited, use the cell's InheritedStyle property.

请参阅See also