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

DataGridView 控件中的每个单元格都可以有自己的样式,例如文本格式、背景色、前景色和字体。 但是,通常情况下,多个单元格将共享特定的样式特征。

共享样式的单元格组可能包括特定行或列中的所有单元格、包含特定值的所有单元格或控件中的所有单元格。 由于这些组重叠,每个单元格可能会从多个位置获取其样式信息。 例如,你可能希望 DataGridView 控件中的每个单元格使用相同的字体,但只有货币列中的单元格使用货币格式,并且只有包含负数的货币单元格才使用红色前景色。

DataGridViewCellStyle 类

DataGridViewCellStyle 类包含以下与视觉样式相关的属性:

此类还包含以下与格式设置相关的属性:

有关这些属性和其他单元格样式属性的详细信息,请参阅 DataGridViewCellStyle 参考文档和以下“另请参阅”部分中列出的主题。

使用 DataGridViewCellStyle 对象

你可以从 DataGridViewDataGridViewColumnDataGridViewRowDataGridViewCell 类及其派生类的各种属性中检索 DataGridViewCellStyle 对象。 如果尚未设置其中某个属性,则检索其值将创建一个新的 DataGridViewCellStyle 对象。 你还可以实例化自己的 DataGridViewCellStyle 对象并将其分配给这些属性。

通过在多个 DataGridView 元素之间共享 DataGridViewCellStyle 对象,可以避免不必要的样式信息重复。 由于在控件、列和行级别设置的样式会向下筛选每个级别,直到单元格级别,因此还可以通过在每个级别仅设置与上面级别不同的样式属性来避免样式重复。 这在后面的“样式继承”部分中有更详细的描述。

下表列出了获取或设置 DataGridViewCellStyle 对象的主要属性。

properties 说明
DefaultCellStyle DataGridViewDataGridViewColumnDataGridViewRow 和派生类 获取或设置整个控件中的所有单元格(包括标题单元格)、列或行中的所有单元格使用的默认样式。
RowsDefaultCellStyle DataGridView 获取或设置控件中所有行使用的默认单元格样式。 这不包括标题单元格。
AlternatingRowsDefaultCellStyle DataGridView 获取或设置控件中交替行使用的默认单元格样式。 用于创建类似账本的效果。
RowHeadersDefaultCellStyle DataGridView 获取或设置控件的行标题使用的默认单元格样式。 如果启用了视觉样式,则被当前主题替代。
ColumnHeadersDefaultCellStyle DataGridView 获取或设置控件的列标题使用的默认单元格样式。 如果启用了视觉样式,则被当前主题替代。
Style DataGridViewCell 和派生类 获取或设置在单元格级别指定的样式。 这些样式会替代从更高级别继承的样式。
InheritedStyle DataGridViewCellDataGridViewRowDataGridViewColumn 和派生类 获取当前应用于单元格、行或列的所有样式,包括从更高级别继承的样式。

如上所述,如果先前未设置样式属性,则获取该属性的值会自动实例化新的 DataGridViewCellStyle 对象。 为避免不必要地创建这些对象,行类和列类具有 HasDefaultCellStyle 属性,你可以检查该属性来确定是否已设置 DefaultCellStyle 属性。 同样,单元格类具有 HasStyle 属性,指示是否已设置 Style 属性。

每个样式属性在 DataGridView 控件上都有一个对应的 PropertyNameChanged 事件。 对于行、列和单元格属性,事件名称以“Row”、“Column”或“Cell”开头(例如,RowDefaultCellStyleChanged)。 当相应的样式属性设置为不同的 DataGridViewCellStyle 对象时,这些事件都会发生。 从样式属性检索 DataGridViewCellStyle 对象并修改其属性值时,这些事件不会发生。 若要响应对单元格样式对象本身的更改,请处理 CellStyleContentChanged 事件。

样式继承

每个 DataGridViewCell 从其 InheritedStyle 属性中获取其外观。 此属性返回的 DataGridViewCellStyle 对象从 DataGridViewCellStyle 类型的属性的层次结构继承其值。 下面按非标题单元格的 InheritedStyle 获取其值的顺序列出了这些属性。

  1. DataGridViewCell.Style

  2. DataGridViewRow.DefaultCellStyle

  3. DataGridView.AlternatingRowsDefaultCellStyle(仅适用于具有奇数索引号的行中的单元格)

  4. DataGridView.RowsDefaultCellStyle

  5. DataGridViewColumn.DefaultCellStyle

  6. DataGridView.DefaultCellStyle

对于行和列标题单元格,InheritedStyle 属性由以下源属性列表中的值按给定顺序填充。

  1. DataGridViewCell.Style

  2. DataGridView.ColumnHeadersDefaultCellStyleDataGridView.RowHeadersDefaultCellStyle

  3. DataGridView.DefaultCellStyle

下图演示了此过程。

Properties of type DataGridViewCellStyle

你还可以访问特定行和列继承的样式。 列的 InheritedStyle 属性从以下属性继承其值。

  1. DataGridViewColumn.DefaultCellStyle

  2. DataGridView.DefaultCellStyle

行的 InheritedStyle 属性从以下属性继承其值。

  1. DataGridViewRow.DefaultCellStyle

  2. DataGridView.AlternatingRowsDefaultCellStyle(仅适用于具有奇数索引号的行中的单元格)

  3. DataGridView.RowsDefaultCellStyle

  4. DataGridView.DefaultCellStyle

对于 InheritedStyle 属性返回的 DataGridViewCellStyle 对象中的每个属性,属性值是从相应列表中的第一个单元格样式获取的,该样式的相应属性设置为 DataGridViewCellStyle 类默认值以外的值。

下表说明了示例单元格的 ForeColor 属性值如何从其包含列继承。

DataGridViewCellStyle 类型的属性 检索到的对象的示例 ForeColor
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 值是列表中的第一个实际值。 它将成为单元格 InheritedStyleForeColor 属性值。

下图说明了不同的 DataGridViewCellStyle 属性如何从不同位置继承其值。

DataGridView property-value inheritance

利用样式继承,可以为整个控件提供适当的样式,而无需在多个位置指定相同的信息。

尽管标题单元格参与了所描述的样式继承,但 DataGridView 控件的 ColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 属性返回的对象具有初始属性值,用于替代 DefaultCellStyle 属性返回的对象的属性值。 如果希望为 DefaultCellStyle 属性返回的对象设置的属性应用于行标题和列标题,则必须将 ColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 属性返回的对象的相应属性设置成为 DataGridViewCellStyle 类指定的默认值。

注意

如果启用了视觉样式,行标题和列标题(TopLeftHeaderCell 除外)将由当前主题自动设置样式,并替代这些属性指定的任何样式。

DataGridViewButtonColumnDataGridViewImageColumnDataGridViewCheckBoxColumn 类型还初始化列的 DefaultCellStyle 属性返回的对象的某些值。 有关详细信息,请参阅这些类型的参考文档。

动态设置样式

若要自定义包含特定值的单元格的样式,请为 DataGridView.CellFormatting 事件实现处理程序。 此事件的处理程序接收 DataGridViewCellFormattingEventArgs 类型的参数。 此对象包含一些属性,通过这些属性可以确定要格式化的单元格的值及其在 DataGridView 控件中的位置。 此对象还包含 CellStyle 属性,该属性初始化为要格式化的单元格的 InheritedStyle 属性值。 你可以修改单元格样式属性,以指定适合单元格值和位置的样式信息。

注意

RowPrePaintRowPostPaint 事件还在事件数据中接收 DataGridViewCellStyle 对象,但在这种情况下,它是行 InheritedStyle 属性的副本,用于只读目的,对其进行更改不会影响控件。

你还可以动态修改单个单元格的样式,以响应 DataGridView.CellMouseEnterCellMouseLeave 等事件。 例如,在 CellMouseEnter 事件的处理程序中,可以存储单元格背景色的当前值(通过单元格的 Style 属性检索),然后将其设置为新颜色,当鼠标悬停在单元格上时,该颜色将突出显示该单元格。 然后,在 CellMouseLeave 事件的处理程序中,可以将背景色恢复为原始值。

注意

无论是否设置了特定样式值,缓存存储在单元格 Style 属性中的值都很重要。 如果临时替换样式设置,将其恢复到原始“未设置”状态可确保单元格恢复为从更高级别继承样式设置。 如果需要确定单元格的实际样式,而不考虑是否继承该样式,请使用单元格的 InheritedStyle 属性。

另请参阅