Windows Form DataGridView 控制項中的儲存格樣式Cell Styles in the Windows Forms DataGridView Control

控制項內的每個資料格都 DataGridView 可以有自己的樣式,例如文字格式、背景色彩、前景色彩和字型。Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. 不過,通常會有多個資料格共用特定的樣式特性。Typically, however, multiple cells will share particular style characteristics.

共用樣式的資料格群組可能包含特定資料列或資料行中的所有資料格、包含特定值的所有資料格,或控制項中的所有資料格。Groups of cells that share styles may include all cells within particular rows or columns, all cells that contain particular values, or all cells in the control. 由於這些群組會重迭,因此每個資料格可能會從一個以上的位置取得其樣式資訊。Because these groups overlap, each cell may get its styling information from more than one place. 例如,您可能希望控制項中的每個資料格都 DataGridView 使用相同的字型,但只有貨幣資料行中的資料格使用貨幣格式,而只有具有負數的貨幣資料格使用紅色前景色彩。For example, you may want every cell in a DataGridView control to use the same font, but only cells in currency columns to use currency format, and only currency cells with negative numbers to use a red foreground color.

DataGridViewCellStyle 類別The DataGridViewCellStyle Class

DataGridViewCellStyle類別包含下列與視覺化樣式相關的屬性:The DataGridViewCellStyle class contains the following properties related to visual style:

此類別也包含下列與格式相關的屬性:This class also contains the following properties related to formatting:

如需這些屬性和其他儲存格樣式屬性的詳細資訊,請參閱 DataGridViewCellStyle 參考檔和下文的「另請參閱」一節中所列的主題。For more information on these properties and other cell-style properties, see the DataGridViewCellStyle reference documentation and the topics listed in the See Also section below.

使用 DataGridViewCellStyle 物件Using DataGridViewCellStyle Objects

您可以 DataGridViewCellStyleDataGridView 、、和類別的各種屬性 DataGridViewColumn DataGridViewRow DataGridViewCell ,以及其衍生類別來取出物件。You can retrieve DataGridViewCellStyle objects from various properties of the DataGridView, DataGridViewColumn, DataGridViewRow, and DataGridViewCell classes and their derived classes. 如果尚未設定這些屬性的其中一個,則抓取其值將會建立新的 DataGridViewCellStyle 物件。If one of these properties has not yet been set, retrieving its value will create a new DataGridViewCellStyle object. 您也可以具現化自己的 DataGridViewCellStyle 物件,並將它們指派給這些屬性。You can also instantiate your own DataGridViewCellStyle objects and assign them to these properties.

您可以藉由在 DataGridViewCellStyle 多個元素之間共用物件,以避免不必要的樣式資訊重複 DataGridViewYou can avoid unnecessary duplication of style information by sharing DataGridViewCellStyle objects among multiple DataGridView elements. 由於在控制項、資料行和資料列層級設定的樣式會向下篩選到資料格層級,因此您也可以在每個層級設定不同于上述層級的樣式屬性,以避免樣式重複。Because the styles set at the control, column, and row levels filter down through each level to the cell level, you can also avoid style duplication by setting only those style properties at each level that differ from the levels above. 這在後續的樣式繼承一節中會更詳細地說明。This is described in more detail in the Style Inheritance section that follows.

下表列出取得或設定物件的主要屬性 DataGridViewCellStyleThe following table lists the primary properties that get or set DataGridViewCellStyle objects.

屬性Property 類別Classes 描述Description
DefaultCellStyle DataGridViewDataGridViewColumnDataGridViewRow 和衍生類別DataGridView, DataGridViewColumn, DataGridViewRow, and derived classes 取得或設定整個控制項中所有儲存格所使用的預設樣式 (包括標頭資料格) 、資料行或資料列中。Gets or sets default styles used by all cells in the entire control (including header cells), in a column, or in a row.
RowsDefaultCellStyle DataGridView 取得或設定控制項中所有資料列所使用的預設儲存格樣式。Gets or sets default cell styles used by all rows in the control. 這不包括標頭儲存格。This does not include header cells.
AlternatingRowsDefaultCellStyle DataGridView 取得或設定控制項中交替資料列所使用的預設儲存格樣式。Gets or sets default cell styles used by alternating rows in the control. 用來建立類似總帳的效果。Used to create a ledger-like effect.
RowHeadersDefaultCellStyle DataGridView 取得或設定控制項的資料列標題所使用的預設儲存格樣式。Gets or sets default cell styles used by the control's row headers. 如果啟用視覺化樣式,則由目前的主題覆寫。Overridden by the current theme if visual styles are enabled.
ColumnHeadersDefaultCellStyle DataGridView 取得或設定控制項的資料行標頭所使用的預設儲存格樣式。Gets or sets default cell styles used by the control's column headers. 如果啟用視覺化樣式,則由目前的主題覆寫。Overridden by the current theme if visual styles are enabled.
Style DataGridViewCell 和衍生類別DataGridViewCell and derived classes 取得或設定在資料格層級指定的樣式。Gets or sets styles specified at the cell level. 這些樣式會覆寫從較高的層級繼承的樣式。These styles override those inherited from higher levels.
InheritedStyle DataGridViewCellDataGridViewRowDataGridViewColumn 和衍生類別DataGridViewCell, DataGridViewRow, DataGridViewColumn, and derived classes 取得目前套用至資料格、資料列或資料行的所有樣式,包括繼承自較高層級的樣式。Gets all the styles currently applied to the cell, row, or column, including styles inherited from higher levels.

DataGridViewCellStyle 先前所述,如果先前尚未設定屬性,取得樣式屬性的值就會自動具現化新的物件。As mentioned above, getting the value of a style property automatically instantiates a new DataGridViewCellStyle object if the property has not been previously set. 若要避免不必要地建立這些物件,資料列和資料行類別具有 HasDefaultCellStyle 屬性,您可加以檢查以判斷屬性是否已 DefaultCellStyle 設定。To avoid creating these objects unnecessarily, the row and column classes have a HasDefaultCellStyle property that you can check to determine whether the DefaultCellStyle property has been set. 同樣地,資料格類別具有 HasStyle 指出屬性是否已設定的屬性 StyleSimilarly, the cell classes have a HasStyle property that indicates whether the Style property has been set.

每個樣式屬性在控制項上都有對應的 PropertyName Changed 事件 DataGridViewEach of the style properties has a corresponding PropertyNameChanged event on the DataGridView control. 針對資料列、資料行和資料格屬性,事件名稱的開頭為 " Row "、" Column " 或 " Cell " (例如 RowDefaultCellStyleChanged) 。For row, column, and cell properties, the name of the event begins with "Row", "Column", or "Cell" (for example, RowDefaultCellStyleChanged). 當對應的樣式屬性設定為不同的物件時,就會發生每個事件 DataGridViewCellStyleEach of these events occurs when the corresponding style property is set to a different DataGridViewCellStyle object. 當您 DataGridViewCellStyle 從樣式屬性取出物件並修改其屬性值時,不會發生這些事件。These events do not occur when you retrieve a DataGridViewCellStyle object from a style property and modify its property values. 若要回應資料格樣式物件本身的變更,請處理 CellStyleContentChanged 事件。To respond to changes to the cell style objects themselves, handle the CellStyleContentChanged event.

樣式繼承Style Inheritance

每個都會 DataGridViewCell 從其屬性取得其外觀 InheritedStyleEach DataGridViewCell gets its appearance from its InheritedStyle property. DataGridViewCellStyle這個屬性所傳回的物件會從型別的屬性階層繼承其值 DataGridViewCellStyleThe DataGridViewCellStyle object returned by this property inherits its values from a hierarchy of properties of type DataGridViewCellStyle. 以下列出這些屬性的順序,如下所示: InheritedStyle 非標頭資料格取得其值的順序。These properties are listed below in the order in which the InheritedStyle for non-header cells obtains its values.

  1. DataGridViewCell.Style

  2. DataGridViewRow.DefaultCellStyle

  3. DataGridView.AlternatingRowsDefaultCellStyle (僅適用于具有奇數索引編號的資料列中的資料格) DataGridView.AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)

  4. DataGridView.RowsDefaultCellStyle

  5. DataGridViewColumn.DefaultCellStyle

  6. DataGridView.DefaultCellStyle

針對資料列和資料行標頭儲存格, InheritedStyle 屬性會依指定順序填入下列來源屬性清單中的值。For row and column header cells, the InheritedStyle property is populated by values from the following list of source properties in the given order.

  1. DataGridViewCell.Style

  2. DataGridView.ColumnHeadersDefaultCellStyleDataGridView.RowHeadersDefaultCellStyleDataGridView.ColumnHeadersDefaultCellStyle or DataGridView.RowHeadersDefaultCellStyle

  3. DataGridView.DefaultCellStyle

下圖說明此程序。The following diagram illustrates this process.

DataGridViewCellStyle 類型的屬性Properties of type DataGridViewCellStyle

您也可以存取特定資料列和資料行所繼承的樣式。You can also access the styles inherited by specific rows and columns. Column InheritedStyle 屬性會從下列屬性繼承其值。The column InheritedStyle property inherits its values from the following properties.

  1. DataGridViewColumn.DefaultCellStyle

  2. DataGridView.DefaultCellStyle

Row InheritedStyle 屬性會從下列屬性繼承其值。The row InheritedStyle property inherits its values from the following properties.

  1. DataGridViewRow.DefaultCellStyle

  2. DataGridView.AlternatingRowsDefaultCellStyle (僅適用于具有奇數索引編號的資料列中的資料格) DataGridView.AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)

  3. DataGridView.RowsDefaultCellStyle

  4. DataGridView.DefaultCellStyle

針對 DataGridViewCellStyle 屬性所傳回之物件中的每一個屬性 InheritedStyle ,會從適當清單中的第一個資料格樣式取得屬性值,該清單中的對應屬性會設定為類別預設值以外的值 DataGridViewCellStyleFor each property in a DataGridViewCellStyle object returned by an InheritedStyle property, the property value is obtained from the first cell style in the appropriate list that has the corresponding property set to a value other than the DataGridViewCellStyle class defaults.

下表說明如何將 ForeColor 範例資料格的屬性值繼承自其包含的資料行。The following table illustrates how the ForeColor property value for an example cell is inherited from its containing column.

型別的屬性 DataGridViewCellStyleProperty of type DataGridViewCellStyle 抓取 ForeColor 物件的範例值Example ForeColor value for retrieved object
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 格資料列的值是清單上的第一個實際值。In this case, the Color.Red value from the cell's row is the first real value on the list. 這會成為 ForeColor 儲存格的屬性值 InheritedStyleThis becomes the ForeColor property value of the cell's InheritedStyle.

下圖說明不同的 DataGridViewCellStyle 屬性如何從不同的位置繼承它們的值。The following diagram illustrates how different DataGridViewCellStyle properties can inherit their values from different places.

DataGridView 屬性-值繼承DataGridView property-value inheritance

藉由利用樣式繼承,您可以為整個控制項提供適當的樣式,而不需要在多個位置指定相同的資訊。By taking advantage of style inheritance, you can provide appropriate styles for the entire control without having to specify the same information in multiple places.

雖然標頭資料格會如所述參與樣式繼承,但控制項的和屬性所傳回的物件 ColumnHeadersDefaultCellStyle RowHeadersDefaultCellStyle DataGridView 具有初始屬性值,可覆寫屬性所傳回之物件的屬性值 DefaultCellStyleAlthough header cells participate in style inheritance as described, the objects returned by the ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle properties of the DataGridView control have initial property values that override the property values of the object returned by the DefaultCellStyle property. 如果您想要將屬性所傳回之物件的屬性設定 DefaultCellStyle 為套用至資料列和資料行標頭,您必須將和屬性所傳回之物件的對應屬性,設定 ColumnHeadersDefaultCellStyle RowHeadersDefaultCellStyle 為該類別所指示的預設值 DataGridViewCellStyleIf you want the properties set for the object returned by the DefaultCellStyle property to apply to row and column headers, you must set the corresponding properties of the objects returned by the ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle properties to the defaults indicated for the DataGridViewCellStyle class.

注意

如果啟用視覺化樣式,則資料列和資料行標頭 (除了 TopLeftHeaderCell) 會自動以目前的主題樣式,並覆寫這些屬性指定的任何樣式。If visual styles are enabled, the row and column headers (except for the TopLeftHeaderCell) are automatically styled by the current theme, overriding any styles specified by these properties.

DataGridViewButtonColumnDataGridViewImageColumnDataGridViewCheckBoxColumn 類型也會初始化資料行屬性所傳回之物件的某些值 DefaultCellStyleThe DataGridViewButtonColumn, DataGridViewImageColumn, and DataGridViewCheckBoxColumn types also initialize some values of the object returned by the column DefaultCellStyle property. 如需詳細資訊,請參閱這些類型的參考檔。For more information, see the reference documentation for these types.

動態設定樣式Setting Styles Dynamically

若要自訂具有特定值的儲存格樣式,請執行事件的處理常式 DataGridView.CellFormattingTo customize the styles of cells with particular values, implement a handler for the DataGridView.CellFormatting event. 此事件的處理常式會接收類型的引數 DataGridViewCellFormattingEventArgsHandlers for this event receive an argument of the DataGridViewCellFormattingEventArgs type. 此物件包含屬性,可讓您判斷要格式化的資料格值,以及其在控制項中的位置 DataGridViewThis object contains properties that let you determine the value of the cell being formatted along with its location in the DataGridView control. 這個物件也包含一個 CellStyle 屬性,這個屬性會初始化為 InheritedStyle 正在格式化之儲存格的屬性值。This object also contains a CellStyle property that is initialized to the value of the InheritedStyle property of the cell being formatted. 您可以修改儲存格樣式屬性,以指定適合資料格值和位置的樣式資訊。You can modify the cell style properties to specify style information appropriate to the cell value and location.

注意

RowPrePaintRowPostPaint 事件也會在 DataGridViewCellStyle 事件資料中接收物件,但在其案例中,它是唯讀用途的資料列 InheritedStyle 屬性複本,而對它的變更不會影響控制項。The RowPrePaint and RowPostPaint events also receive a DataGridViewCellStyle object in the event data, but in their case, it is a copy of the row InheritedStyle property for read-only purposes, and changes to it do not affect the control.

您也可以動態修改個別儲存格的樣式,以回應事件(例如 DataGridView.CellMouseEnterCellMouseLeave 事件)。You can also dynamically modify the styles of individual cells in response to events such as the DataGridView.CellMouseEnter and CellMouseLeave events. 例如,在事件的處理常式中,您可以將資料格 CellMouseEnter 背景色彩的目前值儲存 (透過儲存格的 Style 屬性) 抓取,然後將它設定為新的色彩,以在滑鼠停留在該單元上時反白顯示該儲存格。For example, in a handler for the CellMouseEnter event, you could store the current value of the cell background color (retrieved through the cell's Style property), then set it to a new color that will highlight the cell when the mouse hovers over it. 在事件的處理常式中 CellMouseLeave ,您可以將背景色彩還原為原始值。In a handler for the CellMouseLeave event, you can then restore the background color to the original value.

注意

不論是否已設定特定的樣式值,快取儲存在儲存格屬性中的值 Style 都很重要。Caching the values stored in the cell's Style property is important regardless of whether a particular style value is set. 如果您暫時取代樣式設定,將其還原為其原始的「未設定」狀態,可確保資料格會從較高的層級返回繼承樣式設定。If you temporarily replace a style setting, restoring it to its original "not set" state ensures that the cell will go back to inheriting the style setting from a higher level. 如果您需要判斷資料格的實際效果樣式(不論是否繼承樣式),請使用儲存格的 InheritedStyle 屬性。If you need to determine the actual style in effect for a cell regardless of whether the style is inherited, use the cell's InheritedStyle property.

另請參閱See also