Upravit

Sdílet prostřednictvím


DataGridView.AutoSizeColumnsMode Property

Definition

Gets or sets a value indicating how column widths are determined.

public:
 property System::Windows::Forms::DataGridViewAutoSizeColumnsMode AutoSizeColumnsMode { System::Windows::Forms::DataGridViewAutoSizeColumnsMode get(); void set(System::Windows::Forms::DataGridViewAutoSizeColumnsMode value); };
public System.Windows.Forms.DataGridViewAutoSizeColumnsMode AutoSizeColumnsMode { get; set; }
member this.AutoSizeColumnsMode : System.Windows.Forms.DataGridViewAutoSizeColumnsMode with get, set
Public Property AutoSizeColumnsMode As DataGridViewAutoSizeColumnsMode

Property Value

A DataGridViewAutoSizeColumnsMode value. The default is None.

Exceptions

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

The specified value when setting this property is ColumnHeader, column headers are hidden, and at least one visible column has an AutoSizeMode property value of NotSet.

-or-

The specified value when setting this property is Fill and at least one visible column with an AutoSizeMode property value of NotSet is frozen.

Examples

The following code example illustrates how to use this property in a master/detail 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 column 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

This property lets you configure the control so that column widths are automatically adjusted either to fill the control or to fit cell contents. Size adjustments occur in fill mode whenever the width of the control changes. In content-based sizing modes, size adjustments occur whenever cell contents change or, if WrapMode is enabled, whenever row heights change. Some content-based sizing modes let you limit the size adjustment to the currently displayed rows in order to increase performance.

To change the sizing mode for an individual column, set its AutoSizeMode property. The default value of this property is NotSet, indicating that the column inherits its behavior and its InheritedAutoSizeMode property value from the control.

Columns in fill mode divide the available control width in proportions indicated by their FillWeight property values. The width available for fill mode is determined by subtracting the widths of all other columns from the width of the client area of the control. If this width is smaller than the combined MinimumWidth values of all fill-mode columns, the horizontal scroll bar is displayed, all fill-mode columns are shown with their minimum widths, and user column-resizing is disabled. For more information about column fill mode, see Column Fill Mode in the Windows Forms DataGridView Control.

Only columns with a Visible property value of true are resized automatically, and changing the visibility of a column does not cause resizing to occur. Additionally, when columns are set to automatically resize, the user cannot adjust the column widths with the mouse.

To adjust column widths programmatically, use the AutoResizeColumn or AutoResizeColumns methods or set the column Width property.

For more information about content-based automatic sizing, see Sizing Options in the Windows Forms DataGridView Control.

Applies to

See also