The new DataGridView control for tabular data manipulation provides a large capabilities for its appearance management. The basic class for all visual styles in the DataGridView is the DataGridViewCellStyle. This class implements some properties for changing the font, the background color of the cell, the color of the font, alignments, padding, wrapping modes and so on. Now lets explain the priorities of all cell styles and the styles itself:
1. DataGridView.DefaultCellStyle – This is the default cell style of all cells in the DataGridView control (including row and column header cells). If no other style is defined this style is applied to all cells.
On the second level the DataGridView control is divided on three logical parts. Column Header Cells, Row Header Cells and the sub-table excluding this cells. Lets see the styles for each logical part:
1.1 DataGridView.ColumnHeadersDefaultCellStyle – This is the default cell style for all column header cells including the top left header cells (the top left corner cell of the table). If this style is not defined a DataGridView.DefaultCellStyle is applied for all column header cells.
1.2 DataGridView.RowHeadersDefaultCellStyle – This is the default cell style for all row header cells . If this style is not defined a DataGridView.DefaultCellStyle is applied for all row header cells.
For Column Header and Row Header Cells if the enable header visual styles property of the DataGridView control is set the DataGridView.ColumnHeadersDefaultCellStyle and the DataGridView.RowHeadersDefaultCellStyle are overridden by the current theme.
1.3 DataGridViewColumn.DefaultCellStyle – This is the style used by all cells in the sub-table’s column. For every column in the Columns collection of the DataGridView such style can be defined. If it is not defined a DataGridView.DefaultCellStyle is applied for all cells of the sub-table’s column.
1.3.1 DataGridView.RowsDefaultCellStyle – This is the style used by all cells in the sub-table. If this style is not defined for every column of the sub-table is used its DataGridViewColumn.DefaultCellStyle.
1.3.1.1 DataGridView.AlternatingRowsDefaultCellStyle – This style is used by all cells in the alternating rows in the sub-table, creating a ledger like effect. If this style is not defined a DataGridView.RowsDefaultCellStyle is applied for all alternating row’s cells.
1.3.1.1.1 DataGridViewRow.DefaultCellStyle – This style is used by all cells in a single row of the subtable. Now one row in the subtable can be alternating or regular. If this property is not defined for the single row there are two cases:
- A DataGridView.AlternatingRowsDefaultCellStyle is applied if the row is alternating;
- A DataGridView.RowsDefaultCellStyle is applied if the row is regular;
Every cell of the DataGridView control has its own DataGridViewCell.Style property. If it is set it override every other style on the higher level mentioned above. If it is not set the exact style is DataGridViewCell.InheritedStyle which is examined from all higher level styles.
So if you want to change the style of some cells in the DataGridView control it is very important to understand the styles inheritance and see that you can manage styles on a single cell level or higher (global) level.