Skip to main content

How to: Change Cell Font and Background Color

  • 2 minutes to read

You can change cell font color and cell background.

To specify these attributes for an individual cell, modify the Cell object’s Formatting.Font and Formatting.Fill properties, which are inherited form the Formatting interface.

To change color characteristics for a range of cells, call the CellRange.BeginUpdateFormatting method for this range, modify the Font and Fill properties of the returned Formatting object and call the CellRange.EndUpdateFormatting method to finalize the modification.

Cell Font Color

The Formatting.Font property returns the SpreadsheetFont object. Set this object’s SpreadsheetFont.Color property to the required Color value to change cell font color.

Cell Background

The Formatting.Fill property returns the Fill object. Use the following properties of this object to set cell background.

BackgroundColor
Sets the cell background color. It is also available via the CellRange.FillColor property of the cell or cell range object.
Fill.PatternType
Sets the type of cell background pattern. The available pattern types are accessed via the PatternType enumeration members.
Fill.PatternColor
Sets the shading color for a cell.

To share font color and background settings with multiple cells in a single step, create or modify a style with the Formatting.Font and Formatting.Fill properties specified as required, and assign this style to CellRange.Style for the desired cells. Refer to the following topic for information on how to create and modify a style: How to: Create or Modify a Cell Style

Example

The following example specifies font and background colors for an individual cell and cell range:

View Example: Spreadsheet Document API Examples

using DevExpress.Spreadsheet;
using System.Drawing;
// ...

// Format an individual cell.
worksheet.Cells["A1"].Font.Color = Color.Red;
worksheet.Cells["A1"].FillColor = Color.Yellow;

// Format a cell range.
CellRange range = worksheet.Range["C3:D4"];
Formatting rangeFormatting = range.BeginUpdateFormatting();
rangeFormatting.Font.Color = Color.Blue;
rangeFormatting.Fill.BackgroundColor = Color.LightBlue;
rangeFormatting.Fill.PatternType = PatternType.LightHorizontal;
rangeFormatting.Fill.PatternColor = Color.Violet;
range.EndUpdateFormatting(rangeFormatting);

The image below shows colored cells (the workbook is opened in Microsoft® Excel®).

Spreadsheet_CellColors