DataGridView.AutoResizeColumns 方法

定义

调整所有列的宽度以适应其单元格的内容。Adjusts the width of all columns to fit the contents of their cells.

重载

AutoResizeColumns()

调整所有列的宽度以适应其所有单元格(包括标题单元格)的内容。Adjusts the width of all columns to fit the contents of all their cells, including the header cells.

AutoResizeColumns(DataGridViewAutoSizeColumnsMode)

使用指定的大小调整模式调整所有列的宽度。Adjusts the width of all columns using the specified size mode.

AutoResizeColumns(DataGridViewAutoSizeColumnsMode, Boolean)

使用指定的大小调整模式调整所有列的宽度,还可以选择在行高随后进行调整的假定条件下计算宽度。Adjusts the width of all columns using the specified size mode, optionally calculating the widths with the expectation that row heights will subsequently be adjusted.

AutoResizeColumns()

调整所有列的宽度以适应其所有单元格(包括标题单元格)的内容。Adjusts the width of all columns to fit the contents of all their cells, including the header cells.

public:
 void AutoResizeColumns();
public void AutoResizeColumns ();
member this.AutoResizeColumns : unit -> unit
Public Sub AutoResizeColumns ()

示例

下面的代码示例将调整所有列的大小,以适合列和列标题的内容。The following code example resizes all columns to fit the contents of the columns and the column headers. 此代码示例摘自 [How 中提供的一个更大的示例:以编程方式调整单元格大小以适应 Windows 窗体 DataGridView 控件 @ no__t 中的内容。This code example is part of a larger example provided in How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control.

void SizeAllColumns( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   dataGridView1->AutoResizeColumns( DataGridViewAutoSizeColumnsMode::AllCells );
}


private void SizeAllColumns(Object sender, EventArgs e)
{
    dataGridView1.AutoResizeColumns(
        DataGridViewAutoSizeColumnsMode.AllCells);
}
Private Sub SizeAllColumns(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button6.Click

    DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)

End Sub

注解

如果要控制列的大小调整,此方法非常有用。This method is useful if you want to control when columns resize. 列宽仅在每个方法调用中进行一次调整;如果之后列中的内容发生更改,则这些列将不会自动调整。The column widths are adjusted only once per method call; if the contents of the columns later change, the columns will not automatically adjust. 若要调整特定列的大小,请使用 AutoResizeColumn 方法。To resize a specific column, use the AutoResizeColumn method. 若要将列设置为在其内容更改时自动调整大小,请使用 @no__t 或 @no__t 属性的列。To set the columns to automatically resize whenever their contents change, use the AutoSizeColumnsMode property or the column AutoSizeMode property.

有关以编程方式调整大小的详细信息,请参阅Windows 窗体 DataGridView 控件中的调整大小选项For more information about programmatic resizing, see Sizing Options in the Windows Forms DataGridView Control.

另请参阅

AutoResizeColumns(DataGridViewAutoSizeColumnsMode)

使用指定的大小调整模式调整所有列的宽度。Adjusts the width of all columns using the specified size mode.

public:
 void AutoResizeColumns(System::Windows::Forms::DataGridViewAutoSizeColumnsMode autoSizeColumnsMode);
public void AutoResizeColumns (System.Windows.Forms.DataGridViewAutoSizeColumnsMode autoSizeColumnsMode);
member this.AutoResizeColumns : System.Windows.Forms.DataGridViewAutoSizeColumnsMode -> unit
Public Sub AutoResizeColumns (autoSizeColumnsMode As DataGridViewAutoSizeColumnsMode)

参数

异常

autoSizeColumnsMode 具有值 ColumnHeader,且 ColumnHeadersVisiblefalseautoSizeColumnsMode has the value ColumnHeader and ColumnHeadersVisible is false.

autoSizeColumnsMode 具有值 NoneFillautoSizeColumnsMode has the value None or Fill.

autoSizeColumnsMode 不是有效的 DataGridViewAutoSizeColumnsMode 值。autoSizeColumnsMode is not a valid DataGridViewAutoSizeColumnsMode value.

示例

下面的代码示例将调整所有列的大小,以适合列和列标题的内容。The following code example resizes all columns to fit the contents of the columns and the column headers. 此代码示例摘自 [How 中提供的一个更大的示例:以编程方式调整单元格大小以适应 Windows 窗体 DataGridView 控件 @ no__t 中的内容。This code example is part of a larger example provided in How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control.

void SizeAllColumns( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   dataGridView1->AutoResizeColumns( DataGridViewAutoSizeColumnsMode::AllCells );
}


private void SizeAllColumns(Object sender, EventArgs e)
{
    dataGridView1.AutoResizeColumns(
        DataGridViewAutoSizeColumnsMode.AllCells);
}
Private Sub SizeAllColumns(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button6.Click

    DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)

End Sub

注解

如果要控制列的大小调整,此方法非常有用。This method is useful if you want to control when columns resize. 列宽仅在每个方法调用中进行一次调整;如果之后列中的内容发生更改,则这些列将不会自动调整。The column widths are adjusted only once per method call; if the contents of the columns later change, the columns will not automatically adjust. 若要调整特定列的大小,请使用 AutoResizeColumn 方法。To resize a specific column, use the AutoResizeColumn method. 若要将列设置为在其内容更改时自动调整大小,请使用 @no__t 或 @no__t 属性的列。To set the columns to automatically resize whenever their contents change, use the AutoSizeColumnsMode property or the column AutoSizeMode property.

此重载允许您指定调整大小模式,该模式基于有限单元格集中的值(例如,仅显示的行中的值)计算新的宽度。This overload lets you specify a sizing mode that calculates the new widths based on values in a limited set of cells, such as those in displayed rows only. 这会提高控件包含大量行时的性能。This improves performance when the control contains a large number of rows.

有关以编程方式调整大小的详细信息,请参阅Windows 窗体 DataGridView 控件中的调整大小选项For more information about programmatic resizing, see Sizing Options in the Windows Forms DataGridView Control.

另请参阅

AutoResizeColumns(DataGridViewAutoSizeColumnsMode, Boolean)

使用指定的大小调整模式调整所有列的宽度,还可以选择在行高随后进行调整的假定条件下计算宽度。Adjusts the width of all columns using the specified size mode, optionally calculating the widths with the expectation that row heights will subsequently be adjusted.

protected:
 void AutoResizeColumns(System::Windows::Forms::DataGridViewAutoSizeColumnsMode autoSizeColumnsMode, bool fixedHeight);
protected void AutoResizeColumns (System.Windows.Forms.DataGridViewAutoSizeColumnsMode autoSizeColumnsMode, bool fixedHeight);
member this.AutoResizeColumns : System.Windows.Forms.DataGridViewAutoSizeColumnsMode * bool -> unit
Protected Sub AutoResizeColumns (autoSizeColumnsMode As DataGridViewAutoSizeColumnsMode, fixedHeight As Boolean)

参数

fixedHeight
Boolean

如果要基于当前的行高计算新宽度,则为 true;如果要在行高也进行调整的假定条件下计算宽度,则为 falsetrue to calculate the new widths based on the current row heights; false to calculate the widths with the expectation that the row heights will also be adjusted.

异常

autoSizeColumnsMode 具有值 ColumnHeader,且 ColumnHeadersVisiblefalseautoSizeColumnsMode has the value ColumnHeader and ColumnHeadersVisible is false.

autoSizeColumnsMode 具有值 NoneFillautoSizeColumnsMode has the value None or Fill.

autoSizeColumnsMode 不是有效的 DataGridViewAutoSizeColumnsMode 值。autoSizeColumnsMode is not a valid DataGridViewAutoSizeColumnsMode value.

注解

如果要控制列的大小调整,此方法非常有用。This method is useful if you want to control when columns resize. 列宽仅在每个方法调用中进行一次调整;如果之后列中的内容发生更改,则这些列将不会自动调整。The column widths are adjusted only once per method call; if the contents of the columns later change, the columns will not automatically adjust. 若要调整特定列的大小,请使用 AutoResizeColumn 方法。To resize a specific column, use the AutoResizeColumn method. 若要将列设置为在其内容更改时自动调整大小,请使用 @no__t 或 @no__t 属性的列。To set the columns to automatically resize whenever their contents change, use the AutoSizeColumnsMode property or the column AutoSizeMode property.

此重载允许您指定调整大小模式,该模式基于有限单元格集中的值(例如,仅显示的行中的值)计算新的宽度。This overload lets you specify a sizing mode that calculates the new widths based on values in a limited set of cells, such as those in displayed rows only. 这会提高控件包含大量行时的性能。This improves performance when the control contains a large number of rows.

此重载受到保护,旨在使你能够在派生 @no__t 0 类中实现理想的单元格高度与宽度的比率。This overload is protected and is designed to enable you to achieve ideal cell height-to-width ratios in a derived DataGridView class. 如果 fixedHeight @no__t 为-1,则将计算列宽度,并将调用第二 @no__t 或 @no__t 方法。If fixedHeight is false, the column widths will be calculated with the expectation that you will call the AutoResizeRow or AutoResizeRows method next.

有关以编程方式调整大小的详细信息,请参阅Windows 窗体 DataGridView 控件中的调整大小选项For more information about programmatic resizing, see Sizing Options in the Windows Forms DataGridView Control.

另请参阅

适用于