DataGridViewImageColumn.DefaultCellStyle Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets the column's default cell style.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public override DataGridViewCellStyle DefaultCellStyle { get; set; }

Property Value

Type: System.Windows.Forms.DataGridViewCellStyle

The DataGridViewCellStyle to be applied as the default style.

The default DataGridViewCellStyle returned by this property has the following initial property values:

DataGridViewCellStyle property

Default value




A standard error graphic of type Icon if the initial ValuesAreIcons property value is true; otherwise, a standard error graphic of type Bitmap.

These values override the value specified in the DefaultCellStyle property of the DataGridView control, but may be overridden by other cell style properties. For more information, see Cell Styles in the Windows Forms DataGridView Control.

To prevent the standard error graphic from appearing for null or DBNull.Value cell values, set the DataGridViewCellStyle.NullValue property for this cell style object to null or your own error graphic before adding rows to the control. This does not affect the row for new records, however. To prevent the error graphic from appearing in the row for new records when the control AllowUserToAddRows property value is true, you must also either explicitly set the cell value to null or your own error graphic in a handler for the control RowsAdded event or set the column CellTemplate property to an instance of a DataGridViewImageCell-derived type with an overridden DefaultNewRowValue property that returns null or your own error graphic.

If the NullValue property of the object returned by the DefaultCellStyle property has a value equal to the standard error graphic of type Bitmap, changing the ValuesAreIcons property value to true automatically sets NullValue to the standard error graphic of type Icon. If NullValue has a value equal to the standard error graphic of type Icon, changing the ValuesAreIcons property value to false automatically sets NullValue to the standard error graphic of type Bitmap.

The following code example demonstrates the use of this property.

private void ChangeColumnAlignment()
    songsDataGridView.Columns["Title"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
    songsDataGridView.Columns["Title"].Name = DataGridViewContentAlignment.BottomCenter.ToString();

    songsDataGridView.Columns["Artist"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft;
    songsDataGridView.Columns["Artist"].Name = DataGridViewContentAlignment.BottomLeft.ToString();

    songsDataGridView.Columns["Album"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
    songsDataGridView.Columns["Album"].Name = DataGridViewContentAlignment.BottomRight.ToString();

    songsDataGridView.Columns["Release Date"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    songsDataGridView.Columns["Release Date"].Name = DataGridViewContentAlignment.MiddleCenter.ToString();

    songsDataGridView.Columns["Track"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
    songsDataGridView.Columns["Track"].Name = DataGridViewContentAlignment.MiddleLeft.ToString();

.NET Framework
Available since 2.0
Return to top