Compartilhar via


Cell Styles in the Windows Forms DataGridView Control

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. 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.

The DataGridViewCellStyle Class

The DataGridViewCellStyle class contains the following properties related to visual style:

This class also contains the following properties related to formatting:

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.

Using DataGridViewCellStyle Objects

You can retrieve DataGridViewCellStyle objects from various properties of the DataGridView, DataGridViewColumn, DataGridViewRow, and DataGridViewCell classes and their derived classes. If one of these properties has not yet been set, retrieving its value will create a new DataGridViewCellStyle object. You can also instantiate your own DataGridViewCellStyle objects and assign them to these properties.

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.

The following table lists the primary properties that get or set DataGridViewCellStyle objects.

Property Classes Description

DefaultCellStyle

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 and derived classes

Gets or sets styles specified at the cell level. These styles override those inherited from higher levels.

InheritedStyle

DataGridViewCell, DataGridViewRow, DataGridViewColumn, and derived classes

Gets all the styles currently applied to the cell, row, or column, including styles inherited from higher levels.

As mentioned above, getting the value of a style property automatically instantiates a new DataGridViewCellStyle object if the property has not been previously set. 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. Similarly, the cell classes have a HasStyle property that indicates whether the Style property has been set.

Each of the style properties has a corresponding PropertyNameChanged event on the DataGridView control. For row, column, and cell properties, the name of the event begins with "Row", "Column", or "Cell" (for example, RowDefaultCellStyleChanged). Each of these events occurs when the corresponding style property is set to a different DataGridViewCellStyle object. These events do not occur when you retrieve a DataGridViewCellStyle object from a style property and modify its property values. To respond to changes to the cell style objects themselves, handle the CellStyleContentChanged event.

Style Inheritance

Each DataGridViewCell gets its appearance from its InheritedStyle property. The DataGridViewCellStyle object returned by this property inherits its values from a hierarchy of properties of type DataGridViewCellStyle. These properties are listed below in the order in which the InheritedStyle for non-header cells obtains its values.

  1. System.Windows.Forms.DataGridViewCell.Style

  2. System.Windows.Forms.DataGridViewRow.DefaultCellStyle

  3. System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)

  4. System.Windows.Forms.DataGridView.RowsDefaultCellStyle

  5. System.Windows.Forms.DataGridViewColumn.DefaultCellStyle

  6. System.Windows.Forms.DataGridView.DefaultCellStyle

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. System.Windows.Forms.DataGridViewCell.Style

  2. System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle or System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle

  3. System.Windows.Forms.DataGridView.DefaultCellStyle

The following diagram illustrates this process.

Properties of type DataGridViewCellStyle

You can also access the styles inherited by specific rows and columns. The column InheritedStyle property inherits its values from the following properties.

  1. System.Windows.Forms.DataGridViewColumn.DefaultCellStyle

  2. System.Windows.Forms.DataGridView.DefaultCellStyle

The row InheritedStyle property inherits its values from the following properties.

  1. System.Windows.Forms.DataGridViewRow.DefaultCellStyle

  2. System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)

  3. System.Windows.Forms.DataGridView.RowsDefaultCellStyle

  4. System.Windows.Forms.DataGridView.DefaultCellStyle

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.

The following table illustrates how the ForeColor property value for an example cell is inherited from its containing column.

Property of type DataGridViewCellStyle Example ForeColor value for retrieved object

System.Windows.Forms.DataGridViewCell.Style

System.Drawing.Color.Empty

System.Windows.Forms.DataGridViewRow.DefaultCellStyle

System.Drawing.Color.Red

System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle

System.Drawing.Color.Empty

System.Windows.Forms.DataGridView.RowsDefaultCellStyle

System.Drawing.Color.Empty

System.Windows.Forms.DataGridViewColumn.DefaultCellStyle

System.Drawing.Color.DarkBlue

System.Windows.Forms.DataGridView.DefaultCellStyle

System.Drawing.Color.Black

In this case, the System.Drawing.Color.Red value from the cell's row is the first real value on the list. This becomes the ForeColor property value of the cell's InheritedStyle.

The following diagram illustrates how different DataGridViewCellStyle properties can inherit their values from different places.

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.

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. 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.

NoteNote

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.

The 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

To customize the styles of cells with particular values, implement a handler for the System.Windows.Forms.DataGridView.CellFormatting event. Handlers for this event receive an argument of the DataGridViewCellFormattingEventArgs type. This object contains properties that let you determine the value of the cell being formatted along with its location in the DataGridView control. 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.

NoteNote

The 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.

You can also dynamically modify the styles of individual cells in response to events such as the System.Windows.Forms.DataGridView.CellMouseEnter and CellMouseLeave events. 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. In a handler for the CellMouseLeave event, you can then restore the background color to the original value.

NoteNote

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. 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

Tasks

How to: Set Default Cell Styles for the Windows Forms DataGridView Control

Reference

DataGridView
DataGridViewCellStyle
System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle
System.Windows.Forms.DataGridView.ColumnHeadersDefaultCellStyle
System.Windows.Forms.DataGridView.DefaultCellStyle
System.Windows.Forms.DataGridView.RowHeadersDefaultCellStyle
System.Windows.Forms.DataGridView.RowsDefaultCellStyle
System.Windows.Forms.DataGridViewBand.InheritedStyle
System.Windows.Forms.DataGridViewRow.InheritedStyle
System.Windows.Forms.DataGridViewColumn.InheritedStyle
System.Windows.Forms.DataGridViewBand.DefaultCellStyle
System.Windows.Forms.DataGridViewCell.InheritedStyle
System.Windows.Forms.DataGridViewCell.Style
System.Windows.Forms.DataGridView.CellFormatting
System.Windows.Forms.DataGridView.CellStyleContentChanged
System.Windows.Forms.DataGridView.RowPrePaint
System.Windows.Forms.DataGridView.RowPostPaint

Concepts

Data Formatting in the Windows Forms DataGridView Control

Other Resources

Basic Formatting and Styling in the Windows Forms DataGridView Control