DataGridViewColumn.AutoSizeMode 屬性

定義

取得或設定資料行自動調整其寬度所根據的模式。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

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. 這個程式碼範例是針對類別提供之較大範例的一部分 DataGridViewColumnThis 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 ,這與所有值的屬性值相同, AutoSizeModeNotSet 表示值是繼承自 DataGridView.AutoSizeColumnsMode 屬性(property)。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.

InheritedAutoSizeMode 屬性值為時 Fill ,會將資料行調整大小,以及該模式中的其他資料行。When the InheritedAutoSizeMode property value is Fill, the column is resized along with other columns in that mode. 控制項中的所有填滿模式資料行都會以其屬性值所決定的比例來分割可用的空間 FillWeightAll 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 Forms DataGridView 控制項中的資料行填滿模式For more information about column fill mode, see Column Fill Mode in the Windows Forms DataGridView Control.

InheritedAutoSizeMode 屬性設定為其他任何值(除了以外 None )時,資料行會管理其寬度,使其資料格的值完全顯示而不會裁剪。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 控制項的 AutoResizeColumnAutoResizeColumns 方法,或設定資料行 Width 屬性。To adjust column widths programmatically, use the DataGridView control's AutoResizeColumn or AutoResizeColumns methods or set the column Width property.

如需以內容為基礎的自動調整大小的詳細資訊,請參閱 Windows Forms DataGridView 控制項中的調整大小選項For more information about content-based automatic sizing, see Sizing Options in the Windows Forms DataGridView Control.

適用於

另請參閱