Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataGridView.ColumnHeadersDefaultCellStyle Property

Gets or sets the default column header style.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

'Declaration
Public Property ColumnHeadersDefaultCellStyle As DataGridViewCellStyle
'Usage
Dim instance As DataGridView
Dim value As DataGridViewCellStyle

value = instance.ColumnHeadersDefaultCellStyle

instance.ColumnHeadersDefaultCellStyle = value
/** @property */
public DataGridViewCellStyle get_ColumnHeadersDefaultCellStyle ()

/** @property */
public void set_ColumnHeadersDefaultCellStyle (DataGridViewCellStyle value)

public function get ColumnHeadersDefaultCellStyle () : DataGridViewCellStyle

public function set ColumnHeadersDefaultCellStyle (value : DataGridViewCellStyle)

Not applicable.

Property Value

A DataGridViewCellStyle that represents the default column header style.

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

DataGridViewCellStyle property

Default value

BackColor

The value of the SystemBrushes.Control property.

ForeColor

The value of the SystemBrushes.WindowText property.

SelectionBackColor

The value of the SystemBrushes.Highlight property.

SelectionForeColor

The value of the SystemBrushes.HighlightText property.

Font

The value of the Font property.

Alignment

DataGridViewContentAlignment.MiddleLeft

WrapMode

DataGridViewTriState.True

These values automatically override the values set through the DefaultCellStyle property. To force column headers to inherit the DefaultCellStyle values, you must set the values in the ColumnHeadersDefaultCellStyle object to the default values indicated for the DataGridViewCellStyle class.

For more information about cell style inheritance, see Cell Styles in the Windows Forms DataGridView Control.

If visual styles are enabled and EnableHeadersVisualStyles is set to true, all header cells except the TopLeftHeaderCell are painted using the current theme and the ColumnHeadersDefaultCellStyle values are ignored.

The following code example demonstrates how to set the ColumnHeadersDefaultCellStyle, AutoSizeRowsMode, ColumnHeadersBorderStyle, CellBorderStyle, GridColor, BackgroundColor, RowHeadersVisible, SelectionMode, ColumnCount, and MultiSelect properties. In addition, the code example demonstrates how to set the DataGridViewColumn.Name and DataGridViewColumn.DefaultCellStyle properties. To run this example, paste the code into a form that contains a DataGridView named dataGridView1, and then call the SetUpDataGridView method from the form's constructor or Load event handler. Ensure all events are associated with their event handlers.

Private Sub SetUpDataGridView()

    Me.Controls.Add(dataGridView1)
    dataGridView1.ColumnCount = 5

    With dataGridView1.ColumnHeadersDefaultCellStyle
        .BackColor = Color.Navy
        .ForeColor = Color.White
        .Font = New Font(dataGridView1.Font, FontStyle.Bold)
    End With

    With dataGridView1
        .EditMode = DataGridViewEditMode.EditOnEnter
        .Name = "dataGridView1"
        .Location = New Point(8, 8)
        .Size = New Size(500, 300)
        .AutoSizeRowsMode = _
            DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
        .ColumnHeadersBorderStyle = _
            DataGridViewHeaderBorderStyle.Raised
        .CellBorderStyle = _
            DataGridViewCellBorderStyle.Single
        .GridColor = SystemColors.ActiveBorder
        .RowHeadersVisible = False

        .Columns(0).Name = "Release Date"
        .Columns(1).Name = "Track"
        .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .Columns(2).Name = "Title"
        .Columns(3).Name = "Artist"
        .Columns(4).Name = "Album"

        ' Make the font italic for row four.
        .Columns(4).DefaultCellStyle.Font = _
            New Font(Control.DefaultFont, _
                FontStyle.Italic)

        .SelectionMode = _
            DataGridViewSelectionMode.FullRowSelect
        .MultiSelect = False

        .BackgroundColor = Color.Honeydew

        .Dock = DockStyle.Fill
    End With

End Sub

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

Show:
© 2015 Microsoft