DataGridView 控制項架構 (Windows Form)

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

架構項目

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

Diagram that shows the DataGridViewElement Object Model hierarchy.

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

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

DataGridViewElementStates

DataGridViewElementStates 列舉包含下列值:

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

儲存格和帶狀

控制項 DataGridView 包含兩種基本物件:儲存格和帶狀。 所有儲存格都衍生自 DataGridViewCell 基類。 這兩種波段 DataGridViewColumnDataGridViewRow 兩種都衍生自 DataGridViewBand 基類。

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

DataGridViewCell

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

Diagram that shows the DataGridViewCell Object Model hierarchy.

DataGridViewCell 類型是抽象基類,其衍生自所有儲存格類型。 DataGridViewCell 和其衍生類型不是 Windows Forms 控制項,但有些主控 Windows Forms 控制項。 儲存格所支援的任何編輯功能通常由主控控制項處理。

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

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

DataGridViewColumn

控制項附加資料存放區的架構 DataGridViewDataGridView 以控制項的資料行表示。 您可以使用 集合來存取 DataGridView 控制項的資料 Columns 行。 您可以使用集合來存取選取的資料 SelectedColumns 行。 下列物件模型說明此用法,並顯示 DataGridViewColumn 繼承階層。

Diagram that shows the DataGridViewColumn Object Model hierarchy.

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

下列清單顯示衍生自 DataGridViewColumn 的類別:

DataGridView 編輯控制項

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

Diagram showing the DataGridView Editing Control Object Model hierarchy.

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

如需建立您自己的編輯控制項的詳細資訊,請參閱 How to: Host Controls in Windows Forms DataGridView Cells

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

儲存格類型 主控控制項 欄類型
DataGridViewButtonCell n/a DataGridViewButtonColumn
DataGridViewCheckBoxCell 不適用 DataGridViewCheckBoxColumn
DataGridViewComboBoxCell DataGridViewComboBoxEditingControl DataGridViewComboBoxColumn
DataGridViewImageCell 不適用 DataGridViewImageColumn
DataGridViewLinkCell n/a DataGridViewLinkColumn
DataGridViewTextBoxCell DataGridViewTextBoxEditingControl DataGridViewTextBoxColumn

DataGridViewRow

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

Diagram that shows the DataGridViewRow Object Model hierarchy.

您可以從 類別衍生自己的類型 DataGridViewRow ,雖然這通常不需要。 控制項 DataGridView 有數個數據列相關事件和屬性,可用來自訂其 DataGridViewRow 物件的行為。

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

另請參閱