DataGridViewColumn.AutoSizeMode Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the mode by which the column automatically adjusts its width.
public:
property System::Windows::Forms::DataGridViewAutoSizeColumnMode AutoSizeMode { System::Windows::Forms::DataGridViewAutoSizeColumnMode get(); void set(System::Windows::Forms::DataGridViewAutoSizeColumnMode value); };
public System.Windows.Forms.DataGridViewAutoSizeColumnMode AutoSizeMode { get; set; }
member this.AutoSizeMode : System.Windows.Forms.DataGridViewAutoSizeColumnMode with get, set
Public Property AutoSizeMode As DataGridViewAutoSizeColumnMode
Property Value
A DataGridViewAutoSizeColumnMode value that determines whether the column will automatically adjust its width and how it will determine its preferred width. The default is NotSet.
Exceptions
The specified value when setting this property is a DataGridViewAutoSizeColumnMode that is not valid.
The specified value when setting this property results in an InheritedAutoSizeMode value of ColumnHeader for a visible column when column headers are hidden.
-or-
The specified value when setting this property results in an InheritedAutoSizeMode value of Fill for a visible column that is frozen.
Examples
The following code example forces a column to automatically resize its width to fit its contents. This code example is part of a larger example provided for the DataGridViewColumn class.
// AutoSize the third column.
void Button6_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
DataGridViewColumn^ column = dataGridView->Columns[ 2 ];
column->AutoSizeMode = DataGridViewAutoSizeColumnMode::DisplayedCells;
}
// AutoSize the third column.
private void Button6_Click(object sender,
System.EventArgs e)
{
DataGridViewColumn column = dataGridView.Columns[2];
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
}
' AutoSize the third column.
Private Sub Button6_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button6.Click
Dim column As DataGridViewColumn = dataGridView.Columns(2)
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
End Sub
Remarks
The automatic sizing behavior of the column is determined by the value of the InheritedAutoSizeMode property, which is the same as the value of the AutoSizeMode property for all values except NotSet, which indicates that the value is inherited from the DataGridView.AutoSizeColumnsMode property instead.
When the InheritedAutoSizeMode property value is Fill, the column is resized along with other columns in that mode. All fill-mode columns in the control divide the available space in proportions determined 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.
When the InheritedAutoSizeMode property is set to any other value except None, the column will manage its width so that its cell values are fully displayed without clipping. 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.
Only columns with a Visible property value of true
are resized, 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 DataGridView control's 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.