DataGridViewAutoSizeColumnsMode 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義值來指定要如何調整資料行的寬度。
public enum class DataGridViewAutoSizeColumnsMode
public enum DataGridViewAutoSizeColumnsMode
type DataGridViewAutoSizeColumnsMode =
Public Enum DataGridViewAutoSizeColumnsMode
- 繼承
欄位
AllCells | 6 | 資料行寬度會調整,以適合資料行中的所有儲存格的內容 (包括標題儲存格)。 |
AllCellsExceptHeader | 4 | 資料行寬度會調整,以適合資料行中的所有儲存格的內容 (不包括標題儲存格)。 |
ColumnHeader | 2 | 資料行寬度會調整,以適合資料行行首儲存格的內容。 |
DisplayedCells | 10 | 資料行寬度會調整,以適合資料行中的所有儲存格的內容 (位在目前顯示在螢幕上的資料列中),包括標題儲存格。 |
DisplayedCellsExceptHeader | 8 | 資料行寬度會調整,以適合資料行中的所有儲存格的內容 (位在目前顯示在螢幕上的資料列中),不包括標題儲存格。 |
Fill | 16 | 資料行寬度會調整,使得所有資料行的寬度可以剛好填滿控制項的顯示區,且必須要使用水平捲動方式,才能讓資料行寬度維持在 MinimumWidth 屬性值之上。 相對的資料行寬度是由相對的 FillWeight 屬性值所決定。 |
None | 1 | 資料行寬度不會自動調整。 |
範例
下列程式碼範例說明在主要/詳細資料案例中使用這個列舉,其中兩個控制項會顯示父子關聯性中兩 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
備註
控制項 DataGridView 可以調整其欄的大小,使其填滿控制項的可用水準寬度,或讓它們顯示所有儲存格或指定儲存格的完整內容。
使用 Fill 模式時,資料行的慣用寬度取決於調整該模式中的所有資料行大小,讓控制項中的所有可見資料行完全填滿可用顯示區域的水準寬度。 使用其他模式時,慣用的資料行寬度是顯示所有儲存格或該資料行中儲存格子集的最大儲存格值所需的最小寬度,例如目前顯示的資料列儲存格子集。 使用資料格子集來判斷新的寬度,有助於避免使用許多資料列時的效能負面影響。
以內容為基礎的自動調整大小可防止使用者使用滑鼠調整欄寬。 不過,使用者調整大小會在填滿模式中啟用。
控制項屬性和 AutoResizeColumns 方法會使用此 DataGridView AutoSizeColumnsMode 列舉。
如需調整模式的詳細資訊,請參閱Windows Forms DataGridView 控制項中的調整大小選項。 如需有關資料行填滿模式的詳細資訊,請參閱DataGridView 控制項Windows Forms中的資料行填滿模式。