DataGridView.AutoSizeColumnsMode 属性

定义

获取或设置一个值,该值指示如何确定列宽。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

属性值

异常

当设置此属性时,指定的值不是有效的 DataGridViewAutoSizeColumnsMode 值。The specified value when setting this property is not a valid DataGridViewAutoSizeColumnsMode value.

当设置此属性时,指定值为 ColumnHeader,列标题被隐藏,并且至少有一个可见列具有 NotSetAutoSizeMode 属性值。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- 当设置此属性时,指定值为 Fill,并且至少一个具有 NotSetAutoSizeMode 属性值的可见列被冻结。The specified value when setting this property is Fill and at least one visible column with an AutoSizeMode property value of NotSet is frozen.

示例

下面的代码示例演示如何在主/从方案中使用此属性,这两个 @no__t 0 控件在父/子关系中显示两个表中的数据。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. 在此示例中,主控件的列大小调整模式为 None,列宽以编程方式初始化,以适合加载的值。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). 此示例摘自 [How 中提供的更大示例的一部分:使用两个 Windows 窗体 DataGridView 控件 @ no__t 创建主/详细信息窗体 @。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

注解

此属性可让你配置控件,以使列宽自动调整为填充控件或调整单元格内容。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. 在基于内容的大小调整模式下,无论何时更改单元格内容,都将发生大小调整,如果启用 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.

若要更改单个列的大小调整模式,请设置其 AutoSizeMode 属性。To change the sizing mode for an individual column, set its AutoSizeMode property. 此属性的默认值为 NotSet,指示列继承控件的行为及其 @no__t 属性值。The default value of this property is NotSet, indicating that the column inherits its behavior and its InheritedAutoSizeMode property value from the control.

填充模式中的列以其 FillWeight 属性值指示的比例除以可用控件的宽度。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. 如果此宽度小于所有填充模式列的组合 @no__t 值(0),则将显示水平滚动条,所有填充模式列都以最小宽度显示,并禁用用户列大小调整。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.

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

若要以编程方式调整列宽,请使用 AutoResizeColumn 或 @no__t 方法,或设置列 Width 属性。To adjust column widths programmatically, use the 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.

适用于

另请参阅