控件中的每个 DataGridView 单元格都可以有自己的样式,例如文本格式、背景色、前景色和字体。 但是,通常,多个单元格将共享特定的样式特征。
共享样式的单元格组可能包括特定行或列中的所有单元格、包含特定值的所有单元格或控件中的所有单元格。 由于这些组重叠,因此每个单元格可能会从多个位置获取其样式信息。 例如,你可能希望控件中的每个 DataGridView 单元格使用相同的字体,但只有货币列中的单元格使用货币格式,并且只有带负数的货币单元格才能使用红色前景色。
DataGridViewCellStyle 类
该 DataGridViewCellStyle 类包含以下与视觉样式相关的属性:
此类还包含以下与格式设置相关的属性:
有关这些属性和其他单元格样式属性的详细信息,请参阅 DataGridViewCellStyle 下面的“另请参阅”部分中列出的参考文档和主题。
使用 DataGridViewCellStyle 对象
可以从DataGridViewCellStyle, DataGridView, DataGridViewColumn, 和DataGridViewRow类及其派生类的各种属性中检索DataGridViewCell对象。 如果尚未设置其中一个属性,则检索其值将创建新 DataGridViewCellStyle 对象。 还可以实例化自己的 DataGridViewCellStyle 对象并将其分配给这些属性。
可以通过在多个DataGridViewCellStyle元素之间共享DataGridView对象来避免不必要的样式信息重复。 由于控件、列和行级别设置的样式会逐层传递到单元格级别,因此可以通过仅在每个级别设置与上一级不同的样式属性来避免样式重复。 下面“样式继承”部分更详细地介绍了这一点。
下表列出了获取或设置 DataGridViewCellStyle 对象的主要属性。
资产 | 课程 | DESCRIPTION |
---|---|---|
DefaultCellStyle |
DataGridView、 DataGridViewColumn、 DataGridViewRow和派生类 | 获取或设置整个控件(包括标题单元格)、列中或行中的所有单元格使用的默认样式。 |
RowsDefaultCellStyle | DataGridView | 获取或设置控件中所有行使用的默认单元格样式。 这不包括表头单元格。 |
AlternatingRowsDefaultCellStyle | DataGridView | 获取或设置控件中交替行使用的默认单元格样式。 用于创建类似账本的效果。 |
RowHeadersDefaultCellStyle | DataGridView | 获取或设置控件行标题默认使用的单元格样式。 如果启用了视觉样式,则由当前主题覆盖。 |
ColumnHeadersDefaultCellStyle | DataGridView | 获取或设置控件中列标题所用的默认单元格样式。 如果启用了视觉样式,则由当前主题覆盖。 |
Style | DataGridViewCell 和派生类 | 在单元格级别获取或设置指定的样式。 这些样式替代从较高级别继承的样式。 |
InheritedStyle |
DataGridViewCell、 DataGridViewRow、 DataGridViewColumn和派生类 | 获取当前应用于单元格、行或列的所有样式,包括从较高级别继承的样式。 |
如上所述,如果尚未设置属性,则获取样式属性的值会自动实例化新 DataGridViewCellStyle 对象。 为了避免不必要地创建这些对象,行类和列类具有一个 HasDefaultCellStyle 属性,你可以检查属性 DefaultCellStyle 是否已设定。 同样,单元格类具有一个 HasStyle 属性,指示 Style 是否已设置该属性。
每个样式属性在控件上都有相应的 Changed
DataGridView 事件。 对于行、列和单元格属性,事件的名称以“Row
”、“”Column
或“”Cell
开头(例如, RowDefaultCellStyleChanged)。 当相应的样式属性设置为其他 DataGridViewCellStyle 对象时,将发生上述每个事件。 从样式属性检索 DataGridViewCellStyle 对象并修改其属性值时,不会发生这些事件。 若要响应对单元格样式对象本身的更改,请处理该 CellStyleContentChanged 事件。
样式继承
每个 DataGridViewCell 的外观来自其 InheritedStyle 属性。 DataGridViewCellStyle此属性返回的对象从类型的DataGridViewCellStyle属性层次结构继承其值。 下面列出了这些属性,即非标头单元格获取其值的顺序 InheritedStyle 。
DataGridView.AlternatingRowsDefaultCellStyle (仅适用于具有奇数索引号的行中的单元格)
对于行和列标题单元格,该 InheritedStyle 属性按给定顺序由以下源属性列表中的值填充。
下图说明了此过程。
的属性
您还可以访问特定行和列所继承的样式。 列 InheritedStyle 属性从以下属性继承其值。
行 InheritedStyle 属性从以下属性继承其值。
DataGridView.AlternatingRowsDefaultCellStyle (仅适用于具有奇数索引号的行中的单元格)
对于由DataGridViewCellStyle属性返回的InheritedStyle
对象中的每个属性,属性值是从相应列表中第一个将相应属性设置为非DataGridViewCellStyle类默认值的单元格样式获取的。
下表展示了示例单元格的属性值如何继承自包含它的列。
在这种情况下, Color.Red 单元格行中的值是列表中的第一个实际值。 这将成为 ForeColor 单元格的 InheritedStyle属性值。
下图说明了不同的 DataGridViewCellStyle 属性如何从不同的位置继承其值。
利用样式继承,可以为整个控件提供适当的样式,而无需在多个位置指定相同的信息。
尽管标头单元格参与样式的继承,但在控件 ColumnHeadersDefaultCellStyle 的 RowHeadersDefaultCellStyle 和 DataGridView 属性返回的对象具有初始属性值,这些值会覆盖通过 DefaultCellStyle 属性返回的对象的属性值。 如果希望通过DefaultCellStyle属性返回的对象的属性应用于行标题和列标题,则必须将通过ColumnHeadersDefaultCellStyle和RowHeadersDefaultCellStyle属性返回的对象的相应属性设置为DataGridViewCellStyle类指示的默认值。
注释
如果启用了视觉样式,则行标题和列标题(除外 TopLeftHeaderCell)由当前主题自动设置样式,覆盖这些属性指定的任何样式。
DataGridViewButtonColumn、DataGridViewImageColumn和DataGridViewCheckBoxColumn类型也初始化由列DefaultCellStyle属性返回的对象的某些值。 有关详细信息,请参阅这些类型的参考文档。
动态设置样式
若要自定义具有特定值的单元格样式,请为 DataGridView.CellFormatting 事件实现处理程序。 此事件的处理程序接收 DataGridViewCellFormattingEventArgs 类型的参数。 此对象包含属性,可用于确定要设置格式的单元格的值及其在控件中 DataGridView 的位置。 此对象还包含一个 CellStyle 属性,该属性初始化为所格式化单元格的属性的值 InheritedStyle 。 可以修改单元格样式属性,以指定适合单元格值和位置的样式信息。
注释
事件RowPrePaintRowPostPaint还会在事件数据中接收对象DataGridViewCellStyle,但在他们的情况下,它是行InheritedStyle属性的副本,用于只读,并且对其所做的更改不会影响控件。
还可以动态修改单个单元格的样式,以响应事件(如 DataGridView.CellMouseEnter 和 CellMouseLeave 事件)。 例如,在事件的处理程序 CellMouseEnter 中,可以存储单元格背景色的当前值(通过单元格 Style 的属性检索),然后将其设置为新颜色,当鼠标悬停在单元格上方时,该颜色将突出显示该单元格。 在处理 CellMouseLeave 事件的程序中,可以将背景色还原到原始值。
注释
无论是否设置特定的样式值,缓存单元格 Style 属性中存储的值都很重要。 如果临时替换样式设置,恢复其到原始“未设置”状态,就能确保单元格重新继承来自较高级别的样式设置。 如果需要确定单元格的实际样式,而不考虑是否继承样式,请使用单元格 InheritedStyle 的属性。
另请参阅
- DataGridView
- DataGridViewCellStyle
- DataGridView.AlternatingRowsDefaultCellStyle
- DataGridView.ColumnHeadersDefaultCellStyle
- DataGridView.DefaultCellStyle
- DataGridView.RowHeadersDefaultCellStyle
- DataGridView.RowsDefaultCellStyle
- DataGridViewBand.InheritedStyle
- DataGridViewRow.InheritedStyle
- DataGridViewColumn.InheritedStyle
- DataGridViewBand.DefaultCellStyle
- DataGridViewCell.InheritedStyle
- DataGridViewCell.Style
- DataGridView.CellFormatting
- DataGridView.CellStyleContentChanged
- DataGridView.RowPrePaint
- DataGridView.RowPostPaint
- Windows 窗体 DataGridView 控件中的基本格式和样式设置
- 如何:设置 Windows 窗体 DataGridView 控件的默认单元格样式
- Windows 窗体 DataGridView 控件中的数据格式设置