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屬性的值所決定,這與所有值的AutoSizeMode屬性值相同,但NotSet表示值繼承自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 Forms DataGridView 控制項中的資料行填滿模式For more information about column fill mode, see Column Fill Mode in the Windows Forms DataGridView Control.

當屬性設定為以外None的任何其他值時,資料行會管理其寬度,以便在不裁剪的情況下完全顯示其資料格的值。 InheritedAutoSizeModeWhen 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.

適用於

另請參閱