DataGridView 控制項中的每個儲存格都可以有自己的樣式,例如文字格式、背景色彩、前景色彩及字型。 不過,一般而言,多個儲存格會共用特定的樣式特性。
共用樣式的儲存格群組可能包含特定資料列或資料行內的所有儲存格、包含特定值的所有儲存格,或控制項中的所有儲存格。 由於這些群組重疊,每個儲存格可能會從多個位置取得其樣式資訊。 例如,您可能想要 DataGridView 控制項中的每個儲存格使用相同的字型,但只有貨幣資料行中的儲存格使用貨幣格式,而只有具有負數的貨幣儲存格才能使用紅色前景色彩。
DataGridViewCellStyle 類別
DataGridViewCellStyle 類別包含下列與視覺效果樣式相關的屬性:
此類別也包含下列與格式設定相關的屬性:
如需這些屬性和其他儲存格樣式屬性的詳細資訊,請參閱 DataGridViewCellStyle 參考文件和以下 「另請參閱」 章節中列出的主題。
使用 DataGridViewCellStyle 物件
您可以從 DataGridView、DataGridViewColumn、DataGridViewRow 及 DataGridViewCell 類別及其衍生類別的各種屬性擷取 DataGridViewCellStyle 物件。 如果尚未設定其中一個屬性,擷取其值將會建立新的 DataGridViewCellStyle 物件。 您也可以具現化自己的 DataGridViewCellStyle 物件,並將其指派給這些屬性。
您可以藉由在多個 DataGridView 元素之間共用 DataGridViewCellStyle 物件,以避免不必要的樣式資訊重複。 因為控制項、資料行及資料列層級設定的樣式會向下篩選每個層級至儲存格層級,因此您也可以只設定與上述層級不同的樣式屬性,以避免樣式重複。 這會在後續的樣式繼承一節中詳細說明。
下表列出取得或設定 DataGridViewCellStyle 物件的主要屬性。
房產 | 班級 | 說明 |
---|---|---|
DefaultCellStyle |
DataGridView、DataGridViewColumn、DataGridViewRow 及衍生類別 | 取得或設定整個控制項中所有儲存格所使用的預設樣式 (包括標題儲存格)、資料行或資料列中。 |
RowsDefaultCellStyle | DataGridView | 取得或設定控制項中所有資料列所使用的預設儲存格樣式。 這不包含標頭儲存格。 |
AlternatingRowsDefaultCellStyle | DataGridView | 取得或設定控制項中替代資料列所使用的預設儲存格樣式。 用來建立類似總帳的效果。 |
RowHeadersDefaultCellStyle | DataGridView | 取得或設定控制項的資料列標頭所使用的預設儲存格樣式。 如果已啟用視覺樣式,則由目前主題覆寫。 |
ColumnHeadersDefaultCellStyle | DataGridView | 取得或設定控制項的資料行標頭所使用的預設儲存格樣式。 如果已啟用視覺樣式,則由目前主題覆寫。 |
Style | DataGridViewCell 和衍生類別 | 取得或設定在儲存格層級指定的樣式。 這些樣式會覆寫繼承自較高層級的樣式。 |
InheritedStyle |
DataGridView、DataGridViewColumn、DataGridViewRow 及衍生類別 | 取得目前套用至儲存格、資料列或資料行的所有樣式,包括繼承自較高層級的樣式。 |
如上所述,如果先前尚未設定屬性,取得樣式屬性的值會自動具現化新的 DataGridViewCellStyle 物件。 為了避免不必要地建立這些物件,資料列及資料行類別具有 HasDefaultCellStyle 屬性,您可以檢查該屬性是否已設定 DefaultCellStyle 屬性。 同樣地,儲存格類別具有 HasStyle 屬性,指出是否已設定 Style 屬性。
每個樣式屬性在 DataGridView 控制項上都有對應的 PropertyNameChanged
事件。 對於資料列、資料行及儲存格屬性,事件的名稱開頭為 "Row
", "Column
", 或 "Cell
" (例如,RowDefaultCellStyleChanged)。 當對應的樣式屬性設定為不同的 DataGridViewCellStyle 物件時,就會發生這些事件。 當您從樣式屬性擷取 DataGridViewCellStyle 物件並修改其屬性值時,不會發生這些事件。 若要回應儲存格樣式物件本身的變更,請處理 CellStyleContentChanged 事件。
樣式繼承
每個 DataGridViewCell 都會從其 InheritedStyle 屬性取得其外觀。 這個屬性所傳回的 DataGridViewCellStyle 物件會從類型 DataGridViewCellStyle 屬性的階層繼承其值。 這些屬性會依照非標頭儲存格的 InheritedStyle 取得其值的順序列於下方。
DataGridView.AlternatingRowsDefaultCellStyle (僅適用於具有奇數索引編號之資料列中的儲存格)
對於資料列和資料行標頭儲存格,InheritedStyle 屬性會以指定順序填入下列來源屬性清單的值。
下圖說明此程式。
DataGridViewCellStyle 類型的屬性DataGridViewCells 繼承圖表
您也可以存取特定資料列和資料行所繼承的樣式。 資料行 InheritedStyle 屬性會從下列屬性繼承其值。
資料列 InheritedStyle 屬性會從下列屬性繼承其值。
DataGridView.AlternatingRowsDefaultCellStyle (僅適用於具有奇數索引編號之資料列中的儲存格)
對於 InheritedStyle
屬性所傳回之 DataGridViewCellStyle 物件中的每個屬性,屬性值是從適當清單中的第一個儲存格樣式取得,該樣式會將對應的屬性設定為 DataGridViewCellStyle 類別預設值以外的值。
下表說明範例儲存格的 ForeColor 屬性值如何繼承自其包含的資料行。
在此情況下,儲存格資料列中的 Color.Red 值是清單上的第一個實際值。 這會成為儲存格 InheritedStyle 的 ForeColor 屬性值。
下圖說明不同 DataGridViewCellStyle 屬性如何繼承不同位置的值。
DataGridView 屬性值繼承DataGridViewCells 值繼承圖表
藉由利用樣式繼承,您可以為整個控制項提供適當的樣式,而不必在多個位置指定相同的資訊。
雖然標頭儲存格會如所述參與樣式繼承,但 ColumnHeadersDefaultCellStyle 所傳回的物件和 DataGridView 控制項的 RowHeadersDefaultCellStyle 屬性具有初始屬性值,可覆寫 DefaultCellStyle 屬性所傳回之物件的屬性值。 如果您想要為 DefaultCellStyle 屬性所傳回的物件設定屬性套用至資料列和資料行標頭,您必須將 ColumnHeadersDefaultCellStyle 所傳回之物件的對應屬性,並將屬性 RowHeadersDefaultCellStyle 屬性設定為 DataGridViewCellStyle 類別所指定的預設值。
備註
如果啟用視覺樣式,資料列和資料行標頭 (除了 TopLeftHeaderCell) 會自動由目前的主題設定樣式,並覆寫這些屬性所指定的任何樣式。
DataGridViewButtonColumn、DataGridViewImageColumn 及 DataGridViewCheckBoxColumn 類型也會初始化資料行 DefaultCellStyle 屬性所傳回之物件的一些值。 如需詳細資訊,請參閱這些類型的參考文件。
動態設定樣式
若要自訂具有特定值的儲存格樣式,請實作 DataGridView.CellFormatting 事件的處理常式。 這個事件的處理常式會收到 DataGridViewCellFormattingEventArgs 類型的引數。 這個物件包含屬性,可讓您判斷要格式化的儲存格值及其在 DataGridView 控制項中的位置。 這個物件也包含初始化為格式化儲存格之 InheritedStyle 屬性值的 CellStyle 屬性。 您可以修改儲存格樣式屬性,以指定適合儲存格值和位置的樣式資訊。
備註
RowPrePaint 和 RowPostPaint 事件也會在事件資料中接收 DataGridViewCellStyle 物件,但在事件資料中,它是資料列 InheritedStyle 屬性的複本,而且變更不會影響控制項。
您也可以動態修改個別儲存格的樣式,以回應事件,例如 DataGridView.CellMouseEnter 和 CellMouseLeave 事件。 例如,在 CellMouseEnter 事件的處理常式中,您可以儲存儲存格背景色彩的目前值 (透過儲存格的 Style 屬性擷取),然後將它設定為新的色彩,以在滑鼠停留在儲存格上方時反白顯示儲存格。 在 CellMouseLeave 事件的處理常式中,您可以將背景色彩還原至原始值。
備註
不論是否設定特定樣式值,快取儲存在儲存格 Style 屬性中的值都很重要。 如果您暫時取代樣式設定,請將它還原為其原始的「未設定」狀態,可確保儲存格會回到從較高層級繼承樣式設定。 如果您需要判斷儲存格的實際樣式是否繼承樣式,請使用儲存格的 InheritedStyle 屬性。
另請參閱
- DataGridView
- DataGridViewCellStyle
- DataGridView.AlternatingRowsDefaultCellStyle
- DataGridView.ColumnHeadersDefaultCellStyle
- DataGridView.DefaultCellStyle
- DataGridView.RowHeadersDefaultCellStyle
- DataGridView.RowsDefaultCellStyle
- DataGridViewBand.InheritedStyle
- DataGridViewRow.InheritedStyle
- DataGridViewColumn.InheritedStyle
- DataGridViewBand.DefaultCellStyle
- DataGridViewCell.InheritedStyle
- DataGridViewCell.Style
- DataGridView.CellFormatting
- DataGridView.CellStyleContentChanged
- DataGridView.RowPrePaint
- DataGridView.RowPostPaint
- Windows Forms DataGridView 控制項中的基本格式化和樣式設定
- 操作說明:設定 Windows Forms DataGridView 控制項的預設儲存格樣式
- Windows Forms DataGridView 控制項中的資料格式