DataGridView.AutoSizeColumnsMode 屬性

定義

取得或設定值,表示要如何判斷資料行的寬度。

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 值。 預設為 None

例外狀況

設定這個屬性時所指定的值不是有效的 DataGridViewAutoSizeColumnsMode 值。

設定這個屬性時所指定的值是 ColumnHeader,則資料行標頭會隱藏,而且至少有一個可見資料行具有 NotSetAutoSizeMode 屬性值。

-或-

設定這個屬性時所指定的值是 Fill 且至少有一個具有 NotSetAutoSizeMode 屬性值的可見資料行已凍結。

範例

下列程式碼範例說明如何在主要/詳細資料案例中使用此屬性,其中兩 DataGridView 個控制項會顯示父子關聯性中兩個數據表的資料。 在此範例中,主控制項的資料行大小調整模式為 None ,而且會以程式設計方式初始化資料行寬度以符合載入的值。 詳細資料控制項會設定為自動資料行大小調整模式,因此每當值變更 (時,當使用者變更父資料表中的目前資料列時,資料行就會自動調整) 。 此範例是如何:使用兩個 Windows Forms dataGridView 控制項建立主圖形/詳細資料表單中較大範例的一部分。

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

備註

此屬性可讓您設定 控制項,以便自動調整欄寬以填滿控制項或調整儲存格內容。 每當控制項的寬度變更時,大小調整就會在填滿模式中發生。 在以內容為基礎的調整大小模式中,每當儲存格內容變更或啟用時,每當資料列高度變更 WrapMode 時,就會進行大小調整。 某些以內容為基礎的調整大小模式可讓您限制目前顯示的資料列大小調整,以提高效能。

若要變更個別資料行的大小調整模式,請設定其 AutoSizeMode 屬性。 這個屬性的預設值為 NotSet ,表示資料行會繼承其行為,並從 控制項繼承其 InheritedAutoSizeMode 屬性值。

填滿模式中的資料行會以屬性值所表示 FillWeight 的比例來除以可用的控制項寬度。 填滿模式的寬度取決於從控制項的工作區寬度減去所有其他資料行的寬度。 如果此寬度小於所有填滿模式資料行的合併 MinimumWidth 值,則會顯示水準捲軸,所有填滿模式資料行都會以其最小寬度顯示,而且會停用使用者資料行調整大小。 如需資料行填滿模式的詳細資訊,請參閱DataGridView 控制項中的 Windows Forms資料行填滿模式

只有屬性值 trueVisible 的資料行會自動調整大小,而變更資料行的可見度並不會造成調整大小。 此外,當資料行設定為自動調整大小時,使用者無法使用滑鼠調整欄寬。

若要以程式設計方式調整資料行寬度,請使用 AutoResizeColumnAutoResizeColumns 方法或設定資料行 Width 屬性。

如需內容型自動調整大小的詳細資訊,請參閱Windows Forms DataGridView 控制項中的調整大小選項

適用於

另請參閱