DataGridViewColumn.AutoSizeMode DataGridViewColumn.AutoSizeMode DataGridViewColumn.AutoSizeMode DataGridViewColumn.AutoSizeMode Property

定义

获取或设置模式,通过此模式列可以自动调整其宽度。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

属性值

DataGridViewAutoSizeColumnMode 值确定该列是否将自动调整其宽度,以及该列将如何确定首选宽度。A DataGridViewAutoSizeColumnMode value that determines whether the column will automatically adjust its width and how it will determine its preferred width. 默认值为 NotSetThe default is NotSet.

异常

设置此属性时,指定的值为无效的 DataGridViewAutoSizeColumnModeThe specified value when setting this property is a DataGridViewAutoSizeColumnMode that is not valid.

设置此属性时,如果列标题被隐藏,指定的值会导致可视列的 InheritedAutoSizeModeColumnHeader 值。The specified value when setting this property results in an InheritedAutoSizeMode value of ColumnHeader for a visible column when column headers are hidden.

- 或 --or- 设置此属性时,指定的值会导致冻结的可视列的 InheritedAutoSizeModeFill 值。The specified value when setting this property results in an InheritedAutoSizeMode value of Fill for a visible column that is frozen.

示例

下面的代码示例强制列自动调整其宽度,以适应其内容。The following code example forces a column to automatically resize its width to fit its contents. 此代码示例是为DataGridViewColumn类提供的更大示例的一部分。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

注解

列的自动调整大小行为取决于InheritedAutoSizeMode属性的值,该属性的值与除之外NotSet的所有值的AutoSizeMode属性的值相同,这表示该值继承自DataGridView.AutoSizeColumnsMode属性。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.

当属性值为Fill时,将在该模式下将列与其他列一起调整大小。 InheritedAutoSizeModeWhen the InheritedAutoSizeMode property value is Fill, the column is resized along with other columns in that mode. 控件中的所有填充模式列都按其FillWeight属性值所确定的比例划分可用空间。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. 如果此宽度小于所有填充模式列MinimumWidth的组合值,将显示水平滚动条,所有填充模式列都以最小宽度显示,并禁用用户列大小调整。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. 有关列填充模式的详细信息,请参阅Windows 窗体 DataGridView 控件中的列填充模式For more information about column fill mode, see Column Fill Mode in the Windows Forms DataGridView Control.

None如果将InheritedAutoSizeMode属性设置为除之外的任何其他值,列将管理其宽度,以便在不进行剪裁的情况下完全显示其单元值。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. 在基于内容的大小调整模式下,只要单元格内容发生更改,或者WrapMode只要启用了行高,就会发生大小调整。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.

只有Visible属性值为的true列才会调整大小,更改列的可见性不会导致调整大小。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.

若要以编程方式调整列宽, DataGridView请使用AutoResizeColumn控件AutoResizeColumns的或方法或设置Width列属性。To adjust column widths programmatically, use the DataGridView control's AutoResizeColumn or AutoResizeColumns methods or set the column Width property.

有关基于内容的自动调整大小的详细信息,请参阅Windows 窗体 DataGridView 控件中的调整大小选项For more information about content-based automatic sizing, see Sizing Options in the Windows Forms DataGridView Control.

适用于

另请参阅