dotnetmonitor.com

 
Index
Previous
Next

 

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.

What is the DataGridView
Differences between the DataGridView and DataGrid controls
Highlight of features
Structure of DGV
Architecture Elements
Cells and Bands