Export (0) Print
Expand All
Expand Minimize

DataGridView.AutoSizeColumnsMode Property

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

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

public DataGridViewAutoSizeColumnsMode AutoSizeColumnsMode { get; set; }

ExceptionCondition
InvalidEnumArgumentException

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

InvalidOperationException

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.

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.

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;
}

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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft