共用方式為


DataGridView 控制項架構 (Windows Form)

DataGridView 控件與其相關類別的設計為彈性、可延伸的系統設計,可用來顯示和編輯表格資料。 這些類別全都包含在 System.Windows.Forms 命名空間中,而且全都以「DataGridView」前置詞命名。

架構項目

主要 DataGridView 隨附類別衍生自 DataGridViewElement。 下列物件模型說明 DataGridViewElement 繼承階層。

Diagram that shows the DataGridViewElement Object Model hierarchy.顯示 DataGridViewElement 物件模型階層的圖表。

DataGridViewElement 類別提供父系 DataGridView 控件的參考,並具有 State 屬性,這個屬性會保存值,代表來自 DataGridViewElementStates 列舉的值組合。

下列各節會更詳細地說明這些 DataGridView 隨附類別。

DataGridViewElementStates

DataGridViewElementStates 列舉包含下列值:

這個列舉的值可以與位元邏輯運算子結合,因此 State 屬性可以一次表示多個狀態。 例如,DataGridViewElement 可以同時為 FrozenSelectedVisible

資料格和頻帶

DataGridView 控件包含兩種基本物件:資料格和頻帶結構。 所有資料格衍生自 DataGridViewCell 基底類別。 DataGridViewColumnDataGridViewRow 兩種頻帶都衍生自 DataGridViewBand 基類。

DataGridView 控件與數個類別互通,但最常遇到的是 DataGridViewCellDataGridViewColumnDataGridViewRow

DataGridViewCell

資料格是 DataGridView 的互動基本單位。 顯示會置中於資料格上,而且資料項目通常會透過資料格執行。 您可以使用 DataGridViewRow 類別的 Cells 集合來存取資料格,而且您可以使用 DataGridView 控件的 SelectedCells 集合來存取選取的資料格。 下列物件模型說明此使用方式並顯示 DataGridViewCell 繼承階層。

Diagram that shows the DataGridViewCell Object Model hierarchy.顯示 DataGridViewCell 物件模型階層的圖表。

DataGridViewCell 類型是摘要基底類別,其衍生自所有資料格類型。 DataGridViewCell 及其衍生類型不是 Windows Forms 控件,而是某些主控 Windows Forms 控制件。 資料格所支援的任何編輯功能通常由主控控件處理。

DataGridViewCell 物件不會以與 Windows Forms 控件相同的方式控制自己的外觀和繪製功能。 相反地,DataGridView 會負責其 DataGridViewCell 物件的外觀。 您可以與 DataGridView 控件的屬性和事件互動,大幅影響資料格的外觀和行為。 當您有超出 DataGridView 控件功能之自定義的特殊需求時,您可以實作衍生自 DataGridViewCell 或其其中一個子類別的自有類別。

下列清單列出衍生自 DataGridViewCell 的類別:

DataGridViewColumn

DataGridView 控件附加資料存放區的架構會以 DataGridView 控件的資料行表示。 您可以使用 Columns 集合來存取 DataGridView 控件的資料行。 您可以使用 SelectedColumns 集合來存取控件的資料行。 下列物件模型說明此使用方式並顯示 DataGridViewCell 繼承階層。

Diagram that shows the DataGridViewColumn Object Model hierarchy.顯示 DataGridViewColumn 物件模型階層的圖表。

某些索引鍵資料格類型具有對應的資料行類型。 這些都衍生自 DataGridViewColumn 基底類別。

下列清單列出衍生自 DataGridViewCell 的類別:

DataGridView 編輯控制項

支援進階編輯功能的資料格通常會使用衍生自 Windows Forms 控制件的托管控件。 這些控件也必須實作 IDataGridViewEditingControl 介面。 下列物件模型說明這些控件的使用方式。

Diagram showing the DataGridView Editing Control Object Model hierarchy.顯示 DataGridView 編輯控制件物件模型階層的圖表。

下列編輯控制項會與 DataGridView 控制項一起提供:

如需建立您自己的編輯控制件的詳細資訊,請參閱如何:在 Windows Forms DataGridView 資料格中裝載控件

下表說明資料格類型、資料行類型和編輯控制件之間的關聯性。

資料格類型 主控控件 欄類型
DataGridViewButtonCell n/a DataGridViewButtonColumn
DataGridViewCheckBoxCell n/a DataGridViewCheckBoxColumn
DataGridViewComboBoxCell DataGridViewComboBoxEditingControl DataGridViewComboBoxColumn
DataGridViewImageCell n/a DataGridViewImageColumn
DataGridViewLinkCell n/a DataGridViewLinkColumn
DataGridViewTextBoxCell DataGridViewTextBoxEditingControl DataGridViewTextBoxColumn

DataGridViewRow

DataGridViewRow 類別會顯示從附加 DataGridView 控件的資料存放區記錄資料欄位。 您可以使用 Rows 集合來存取 DataGridView 控件的資料列。 您可以使用 SelectedRows 集合來存取控件的資料列。 下列物件模型說明此使用方式並顯示 DataGridViewCell 繼承階層。

Diagram that shows the DataGridViewRow Object Model hierarchy.顯示 DataGridViewRow 物件模型階層的圖表。

您可以從 DataGridViewRow 類別衍生自己的類型,不過這通常不需要。 DataGridView 控件有數個資料列相關事件和屬性,可用來自定義其 DataGridViewRow 對象的行為。

如果您啟用 DataGridView 控件的 AllowUserToAddRows 屬性,新增新資料列的特殊資料列會顯示為最後一個資料列。 此資料列是 Rows 集合的一部分,但具有可能需要注意的特殊功能。 如需更多資訊,請參閱使用 Windows Form DataGridView 控制項中用於新增資料錄的資料列

另請參閱