How to: Change Cell Font and Background Color
- 2 minutes to read
You can change the font and background colors of a cell.
To specify these attributes for an individual cell, modify the Formatting.Font and Formatting.Fill properties of the Cell object. These properties are inherited from the Formatting interface.
To change the color characteristics for a range of cells, call the Range.BeginUpdateFormatting method for this range, modify the Font and Fill properties of the returned Formatting object, and call the Range.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.
- Fill.BackgroundColor - sets the cell background color. It is also available via the Range.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.
You can also fill a cell background with a color gradient of different types. See the How to: Apply Gradient Fill example.
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 Range.Style for the desired cells.
This example demonstrates how to format color characteristics (font and background colors) for an individual cell and range of cells.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-spreadsheetcontrol-api-e4655
// Format an individual cell.
worksheet.Cells["A1"].Font.Color = Color.Red;
worksheet.Cells["A1"].FillColor = Color.Yellow;
// Format a range of cells.
Range range = worksheet.Range["C3:H10"];
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 the results.