編輯

共用方式為


DataGridViewAutoSizeColumnsMode Enum

Definition

Defines values for specifying how the widths of columns are adjusted.

public enum class DataGridViewAutoSizeColumnsMode
public enum DataGridViewAutoSizeColumnsMode
type DataGridViewAutoSizeColumnsMode = 
Public Enum DataGridViewAutoSizeColumnsMode
Inheritance
DataGridViewAutoSizeColumnsMode

Fields

Name Value Description
None 1

The column widths do not automatically adjust.

ColumnHeader 2

The column widths adjust to fit the contents of the column header cells.

AllCellsExceptHeader 4

The column widths adjust to fit the contents of all cells in the columns, excluding header cells.

AllCells 6

The column widths adjust to fit the contents of all cells in the columns, including header cells.

DisplayedCellsExceptHeader 8

The column widths adjust to fit the contents of all cells in the columns that are in rows currently displayed onscreen, excluding header cells.

DisplayedCells 10

The column widths adjust to fit the contents of all cells in the columns that are in rows currently displayed onscreen, including header cells.

Fill 16

The column widths adjust so that the widths of all columns exactly fill the display area of the control, requiring horizontal scrolling only to keep column widths above the MinimumWidth property values. Relative column widths are determined by the relative FillWeight property values.

Examples

The following code example illustrates the use of this enumeration in a master/details scenario where two DataGridView controls display data from two tables in a parent/child relationship. In this example, the column sizing mode for the master control is None and the column widths are programmatically initialized to fit the loaded values. The details control is set to an automatic sizing mode so that columns will adjust automatically whenever the values change (for example, when the user changes the current row in the parent table). This example is part of a larger example available in How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls.

private void Form1_Load(object sender, System.EventArgs e)
{
    // Bind the DataGridView controls to the BindingSource
    // components and load the data from the database.
    masterDataGridView.DataSource = masterBindingSource;
    detailsDataGridView.DataSource = detailsBindingSource;
    GetData();

    // Resize the master DataGridView columns to fit the newly loaded data.
    masterDataGridView.AutoResizeColumns();

    // Configure the details DataGridView so that its columns automatically
    // adjust their widths when the data changes.
    detailsDataGridView.AutoSizeColumnsMode = 
        DataGridViewAutoSizeColumnsMode.AllCells;
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load

    ' Bind the DataGridView controls to the BindingSource
    ' components and load the data from the database.
    masterDataGridView.DataSource = masterBindingSource
    detailsDataGridView.DataSource = detailsBindingSource
    GetData()

    ' Resize the master DataGridView columns to fit the newly loaded data.
    masterDataGridView.AutoResizeColumns()

    ' Configure the details DataGridView so that its columns automatically
    ' adjust their widths when the data changes.
    detailsDataGridView.AutoSizeColumnsMode = _
        DataGridViewAutoSizeColumnsMode.AllCells

End Sub

Remarks

The DataGridView control can resize its columns to make them fill the available horizontal width of the control or to make them display the full contents of all cells or of specified cells.

With Fill mode, the preferred width of a column is determined by resizing all columns in that mode so that all visible columns in the control exactly fill the horizontal width of the available display area. With other modes, the preferred column width is the minimum width required to display the largest cell value from all cells or a subset of cells in that column, such as the subset of cells that are in currently displayed rows. Using a subset of cells to determine the new width is useful to avoid a performance penalty when working with many rows of data.

Content-based automatic resizing prevents users from adjusting column widths with the mouse. User resizing is enabled in fill mode, however.

This enumeration is used by the DataGridView control AutoSizeColumnsMode property and AutoResizeColumns method.

For more information about sizing modes, see Sizing Options in the Windows Forms DataGridView Control. For more information about column fill mode in particular, see Column Fill Mode in the Windows Forms DataGridView Control.

Applies to

See also