Windows Form DataGridView 控制項中的儲存格樣式

控制項中的每個 DataGridView 儲存格都可以有自己的樣式,例如文字格式、背景色彩、前景色彩和字型。 不過,一般而言,多個儲存格會共用特定的樣式特性。

共用樣式的儲存格群組可能包含特定資料列或資料行內的所有儲存格、包含特定值的所有儲存格,或控制項中的所有儲存格。 由於這些群組重迭,每個儲存格可能會從多個位置取得其樣式資訊。 例如,您可能希望控制項中的每個 DataGridView 儲存格使用相同的字型,但只有貨幣資料行中的儲存格使用貨幣格式,而只有具有負數的貨幣儲存格才能使用紅色前景色彩。

DataGridViewCellStyle 類別

類別 DataGridViewCellStyle 包含下列與視覺化樣式相關的屬性:

此類別也包含下列與格式設定相關的屬性:

如需這些屬性和其他儲存格樣式屬性的詳細資訊,請參閱 DataGridViewCellStyle 下面的一節中所列的參考檔和主題。

使用 DataGridViewCellStyle 物件

您可以從 、 DataGridViewColumnDataGridViewRowDataGridViewCell 類別及其衍生類別的各種屬性 DataGridView 擷取 DataGridViewCellStyle 物件。 如果尚未設定其中一個屬性,擷取其值將會建立新的 DataGridViewCellStyle 物件。 您也可以具現化自己的 DataGridViewCellStyle 物件,並將其指派給這些屬性。

您可以藉由在多個 DataGridView 元素之間共用 DataGridViewCellStyle 物件,以避免不必要的樣式資訊重複。 因為控制項、資料行和資料列層級設定的樣式會向下篩選每個層級至儲存格層級,因此您也可以只設定與上述層級不同的樣式屬性,以避免樣式重複。 這會在後續的樣式繼承一節中詳細說明。

下表列出取得或設定 DataGridViewCellStyle 物件的主要屬性。

屬性 類別 描述
DefaultCellStyle DataGridViewDataGridViewColumnDataGridViewRow 、 和衍生類別 取得或設定整個控制項中所有儲存格所使用的預設樣式(包括標題儲存格)、資料行或資料列中。
RowsDefaultCellStyle DataGridView 取得或設定 控制項中所有資料列所使用的預設儲存格樣式。 這不包含標頭儲存格。
AlternatingRowsDefaultCellStyle DataGridView 取得或設定 控制項中替代資料列所使用的預設儲存格樣式。 用來建立類似總帳的效果。
RowHeadersDefaultCellStyle DataGridView 取得或設定控制項的資料列標頭所使用的預設儲存格樣式。 如果已啟用視覺化樣式,則由目前主題覆寫。
ColumnHeadersDefaultCellStyle DataGridView 取得或設定控制項資料行標頭所使用的預設儲存格樣式。 如果已啟用視覺化樣式,則由目前主題覆寫。
Style DataGridViewCell 和衍生類別 取得或設定在儲存格層級指定的樣式。 這些樣式會覆寫繼承自較高層級的樣式。
InheritedStyle DataGridViewCellDataGridViewRowDataGridViewColumn 、 和衍生類別 取得目前套用至儲存格、資料列或資料行的所有樣式,包括繼承自較高層級的樣式。

如上所述,如果先前尚未設定屬性,取得樣式屬性的值會自動具現化新的 DataGridViewCellStyle 物件。 為了避免不必要地建立這些物件,資料列和資料行類別具有 HasDefaultCellStyle 屬性,您可以檢查屬性是否已 DefaultCellStyle 設定。 同樣地,儲存格類別具有 HasStyle 屬性,指出是否已 Style 設定屬性。

每個樣式屬性在 控制項上 DataGridView 都有對應的 PropertyName Changed 事件。 對於資料列、資料行和儲存格屬性,事件的名稱會以 「 Row 」、「」 Column 或 「 Cell 」 開頭(例如 , RowDefaultCellStyleChanged 。 當對應的樣式屬性設定為不同的 DataGridViewCellStyle 物件時,就會發生上述每個事件。 當您從樣式屬性擷取 DataGridViewCellStyle 物件並修改其屬性值時,不會發生這些事件。 若要回應儲存格樣式物件本身的變更,請處理 CellStyleContentChanged 事件。

樣式繼承

每個都會 DataGridViewCell 從其 屬性取得其 InheritedStyle 外觀。 DataGridViewCellStyle這個屬性所傳回的物件會從 類型的 DataGridViewCellStyle 屬性階層繼承其值。 這些屬性會列在下列順序 InheritedStyle ,讓非標頭儲存格取得其值。

  1. DataGridViewCell.Style

  2. DataGridViewRow.DefaultCellStyle

  3. DataGridView.AlternatingRowsDefaultCellStyle (僅適用于具有奇數索引編號的資料列儲存格)

  4. DataGridView.RowsDefaultCellStyle

  5. DataGridViewColumn.DefaultCellStyle

  6. DataGridView.DefaultCellStyle

對於資料列和資料行標頭資料格, InheritedStyle 屬性會以指定順序填入下列來源屬性清單的值。

  1. DataGridViewCell.Style

  2. DataGridView.ColumnHeadersDefaultCellStyleDataGridView.RowHeadersDefaultCellStyle

  3. DataGridView.DefaultCellStyle

下圖說明此流程。

Properties of type DataGridViewCellStyle

您也可以存取特定資料列和資料行所繼承的樣式。 資料行 InheritedStyle 屬性會從下列屬性繼承其值。

  1. DataGridViewColumn.DefaultCellStyle

  2. DataGridView.DefaultCellStyle

資料列 InheritedStyle 屬性會從下列屬性繼承其值。

  1. DataGridViewRow.DefaultCellStyle

  2. DataGridView.AlternatingRowsDefaultCellStyle (僅適用于具有奇數索引編號的資料列儲存格)

  3. DataGridView.RowsDefaultCellStyle

  4. DataGridView.DefaultCellStyle

對於屬性所 InheritedStyle 傳回之物件中的每個 DataGridViewCellStyle 屬性,屬性值是從適當清單中的第一個儲存格樣式取得,該樣式會將對應的屬性設定為類別預設值以外的 DataGridViewCellStyle 值。

下表說明範例儲存格的屬性值如何 ForeColor 繼承自其包含的資料行。

類型的屬性 DataGridViewCellStyle 擷取物件的範例 ForeColor
DataGridViewCell.Style Color.Empty
DataGridViewRow.DefaultCellStyle Color.Red
DataGridView.AlternatingRowsDefaultCellStyle Color.Empty
DataGridView.RowsDefaultCellStyle Color.Empty
DataGridViewColumn.DefaultCellStyle Color.DarkBlue
DataGridView.DefaultCellStyle Color.Black

在此情況下, Color.Red 儲存格資料列的值是清單上的第一個實際值。 這會成為 ForeColor 儲存格 的 InheritedStyle 屬性值。

下圖說明不同 DataGridViewCellStyle 屬性如何從不同位置繼承其值。

DataGridView property-value inheritance

藉由利用樣式繼承,您可以為整個控制項提供適當的樣式,而不必在多個位置指定相同的資訊。

雖然標頭儲存格會如所述參與樣式繼承,但是 控制項 的 ColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 屬性 DataGridView 所傳回的物件具有初始屬性值,可覆寫 屬性所 DefaultCellStyle 傳回之物件的屬性值。 如果您想要為 屬性所 DefaultCellStyle 傳回的物件設定屬性套用至資料列和資料行標頭,您必須將 和 RowHeadersDefaultCellStyle 屬性所 ColumnHeadersDefaultCellStyle 傳回之 物件的對應屬性設定為 類別所 DataGridViewCellStyle 指定的預設值。

注意

如果啟用視覺化樣式,資料列和資料行標頭(除了 TopLeftHeaderCell 除外)會自動由目前的主題設定樣式,並覆寫這些屬性所指定的任何樣式。

DataGridViewButtonColumnDataGridViewImageColumnDataGridViewCheckBoxColumn 型別也會初始化資料行 DefaultCellStyle 屬性所傳回之物件的一些值。 如需詳細資訊,請參閱這些類型的參考檔。

動態設定樣式

若要自訂具有特定值的儲存格樣式,請實作 DataGridView.CellFormatting 事件的處理常式。 此事件的處理常式會收到 類型的引數 DataGridViewCellFormattingEventArgs 。 這個物件包含屬性,可讓您判斷要格式化之儲存格的值,以及控制項中 DataGridView 的位置。 這個物件也包含 CellStyle 屬性,這個屬性會初始化為 InheritedStyle 格式化之儲存格的 屬性值。 您可以修改儲存格樣式屬性,以指定適合儲存格值和位置的樣式資訊。

注意

RowPrePaintRowPostPaint 事件也會在事件資料中接收 DataGridViewCellStyle 物件,但在其案例中,它是資料列 InheritedStyle 屬性的複本,用於唯讀用途,而且變更不會影響控制項。

您也可以動態修改個別儲存格的樣式,以回應 和 CellMouseLeave 事件等 DataGridView.CellMouseEnter 事件。 例如,在 事件的處理常式中 CellMouseEnter ,您可以儲存儲存格背景色彩的目前值(透過 Style 儲存格的 屬性擷取),然後將它設定為新的色彩,以在滑鼠停留在儲存格上方時反白顯示儲存格。 在 事件的處理常式中 CellMouseLeave ,您接著可以將背景色彩還原至原始值。

注意

不論是否已設定特定樣式值,快取儲存在儲存格 Style 屬性中的值都很重要。 如果您暫時取代樣式設定,請將它還原為其原始的「未設定」狀態,可確保儲存格會回到從較高層級繼承樣式設定。 如果您需要判斷儲存格的實際樣式,不論是否繼承樣式,請使用儲存格的 InheritedStyle 屬性。

另請參閱