Back to Devexpress

How to: Format a Cell or Range of Cells

wpf-16380-controls-and-libraries-spreadsheet-examples-formatting-how-to-format-a-cell-or-range-of-cells.md

latest5.5 KB
Original Source

How to: Format a Cell or Range of Cells

  • Oct 27, 2021
  • 3 minutes to read

The actual cell appearance is determined by the format settings specified by the applied style and the cell’s format settings. Each of these formatting types provides a set of flags (Formatting.Flags). Each flag corresponds to a specific group of format attributes. These flags determine whether to use attributes specified in the applied style or attributes specified directly for the cell.

GroupAttributesFlag
AlignmentHorizontal and vertical alignment of cell content, indentation and text wrap.StyleFlags.Alignment
BordersCell border line styles and colors.StyleFlags.Borders
FillCell background color.StyleFlags.Fill
FontCell font settings (name, style, color and size).StyleFlags.Font
Number FormatCell number format.StyleFlags.Number
ProtectionCell protection options (Locked and Hidden).StyleFlags.Protection

This example demonstrates how to format cells in a worksheet.

View Example

csharp
// Access the cell to be formatted.
Cell cell = worksheet.Cells["B2"];

// Specify font settings (font name, color, size and style).
cell.Font.Name = "MV Boli";
cell.Font.Color = Color.Blue;
cell.Font.Size = 14;
cell.Font.FontStyle = SpreadsheetFontStyle.Bold;

// Specify cell background color.
cell.Fill.BackgroundColor = Color.LightSkyBlue;

// Specify text alignment in the cell. 
cell.Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
cell.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
// Access the range of cells to be formatted.
CellRange range = worksheet.Range["C3:E6"];

// Begin updating of the range formatting. 
Formatting rangeFormatting = range.BeginUpdateFormatting();

// Specify font settings (font name, color, size and style).
rangeFormatting.Font.Name = "MV Boli";
rangeFormatting.Font.Color = Color.Blue;
rangeFormatting.Font.Size = 14;
rangeFormatting.Font.FontStyle = SpreadsheetFontStyle.Bold;

// Specify cell background color.
rangeFormatting.Fill.BackgroundColor = Color.LightSkyBlue;

// Specify text alignment in cells.
rangeFormatting.Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
rangeFormatting.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;

// End updating of the range formatting.
range.EndUpdateFormatting(rangeFormatting);
vb
' Access the cell to be formatted.
Dim cell As Cell = worksheet.Cells("B2")

' Specify font settings (font name, color, size and style).
cell.Font.Name = "MV Boli"
cell.Font.Color = Color.Blue
cell.Font.Size = 14
cell.Font.FontStyle = SpreadsheetFontStyle.Bold

' Specify cell background color.
cell.Fill.BackgroundColor = Color.LightSkyBlue

' Specify text alignment in the cell. 
cell.Alignment.Vertical = SpreadsheetVerticalAlignment.Center
cell.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
' Access the range of cells to be formatted.
Dim range As CellRange = worksheet.Range("C3:E6")

' Begin updating of the range formatting. 
Dim rangeFormatting As Formatting = range.BeginUpdateFormatting()

' Specify font settings (font name, color, size and style).
rangeFormatting.Font.Name = "MV Boli"
rangeFormatting.Font.Color = Color.Blue
rangeFormatting.Font.Size = 14
rangeFormatting.Font.FontStyle = SpreadsheetFontStyle.Bold

' Specify cell background color.
rangeFormatting.Fill.BackgroundColor = Color.LightSkyBlue

' Specify text alignment in cells.
rangeFormatting.Alignment.Vertical = SpreadsheetVerticalAlignment.Center
rangeFormatting.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center

' End updating of the range formatting.
range.EndUpdateFormatting(rangeFormatting)

See Also

How to: Load and Use Custom Fonts in the Spreadsheet Control

How to: Apply Rich Formatting to Cell Text