Export (0) Print
Expand All
Expand Minimize

DataGridView.ColumnHeadersHeightSizeMode Property

Gets or sets a value indicating whether the height of the column headers is adjustable and whether it can be adjusted by the user or is automatically adjusted to fit the contents of the headers.

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

'Declaration
Public Property ColumnHeadersHeightSizeMode As DataGridViewColumnHeadersHeightSizeMode

Property Value

Type: System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode
A DataGridViewColumnHeadersHeightSizeMode value indicating the mode by which the height of the column headers row can be adjusted. The default is EnableResizing.

ExceptionCondition
InvalidEnumArgumentException

The specified value when setting this property is not a valid DataGridViewColumnHeadersHeightSizeMode value.

When this property is set to AutoSize, the height of the column headers cannot be adjusted by the user.

To adjust the height of the column headers programmatically, use the AutoResizeColumnHeadersHeight method or set the ColumnHeadersHeight property.

To set the sizing mode for the row headers, use the RowHeadersWidthSizeMode property.

For cell contents to wrap onto multiple lines when the column headers are resized, the cell style in effect for the cell must have a WrapMode property value of True.

For more information about header sizing, see Sizing Options in the Windows Forms DataGridView Control.

NoteNote

The DataGridView control does not support double buffering. If DoubleBuffered is set to true in a derived DataGridView control, users will not receive visual feedback when resizing rows, columns, or headers or when reordering columns.

The following code example illustrates how to use this property in a DataGridView control intended primarily for display. In this example, the visual appearance of the control is customized in several ways and the control is configured for limited interactivity. This example is part of a larger example available in the DataGridViewCellStyle class overview.

' Configures the appearance and behavior of a DataGridView control. 
Private Sub InitializeDataGridView()

    ' Initialize basic DataGridView properties.
    dataGridView1.Dock = DockStyle.Fill
    dataGridView1.BackgroundColor = Color.LightGray
    dataGridView1.BorderStyle = BorderStyle.Fixed3D

    ' Set property values appropriate for read-only display and  
    ' limited interactivity. 
    dataGridView1.AllowUserToAddRows = False
    dataGridView1.AllowUserToDeleteRows = False
    dataGridView1.AllowUserToOrderColumns = True
    dataGridView1.ReadOnly = True
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    dataGridView1.MultiSelect = False
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
    dataGridView1.AllowUserToResizeColumns = False
    dataGridView1.ColumnHeadersHeightSizeMode = _
        DataGridViewColumnHeadersHeightSizeMode.DisableResizing
    dataGridView1.AllowUserToResizeRows = False
    dataGridView1.RowHeadersWidthSizeMode = _
        DataGridViewRowHeadersWidthSizeMode.DisableResizing

    ' Set the selection background color for all the cells.
    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
    dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black

    ' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default 
    ' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
    dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty

    ' Set the background color for all rows and for alternating rows.  
    ' The value for alternating rows overrides the value for all rows. 
    dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray
    dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray

    ' Set the row and column header styles.
    dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
    dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black

    ' Set the Format property on the "Last Prepared" column to cause 
    ' the DateTime to be formatted as "Month, Year".
    dataGridView1.Columns("Last Prepared").DefaultCellStyle.Format = "y" 

    ' Specify a larger font for the "Ratings" column.  
    Dim font As New Font( _
        dataGridView1.DefaultCellStyle.Font.FontFamily, 25, FontStyle.Bold)
    Try
        dataGridView1.Columns("Rating").DefaultCellStyle.Font = font
    Finally
        font.Dispose()
    End Try 

End Sub

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft