Skip to main content

Cell Style Customization

  • 5 minutes to read

A cell style can be customized via the Style property of the TdxSpreadSheetCell object that corresponds to a cell within a worksheet, and can be accessed via the Cells property of the TdxSpreadSheetTableView object. In order to access the desired worksheet of the ExpresSpreadSheet control, you can use the Sheets or ActiveSheet properties of the TdxSpreadSheet class.

The Style property of the cell returns a TdxSpreadSheetCellStyle class instance that contains properties whose values correspond to the font, content alignment, foreground and background color, and fill pattern of the cell.

Refer to one of these subtopics for more information:

  • Change Font Properties;

  • Align Data within a Cell;

  • Apply Borders to a Cell;

  • Apply Fill Pattern to a Cell.

Change Font Properties

Font properties can be accessed via the Font property of the TdxSpreadSheetCellStyle object. The Font property references a TdxSpreadSheetCellFont object. This object has the Charset, Color, Height, Name, Pitch, Size, and Style properties, which specify the character set, typeface, size, style and color of the font used to display a cell value.

The following code example customizes font settings for the B2 cell in the active worksheet.

var
  ACell: TdxSpreadSheetCell;
//...
  ACell := dxSpreadSheet1.ActiveSheetAsTable.CreateCell(1, 1);
  ACell.SetText('www.devexpress.com');
  ACell.Style.Font.Color := clBlue;
  ACell.Style.Font.Name := 'Century Gothic';
  ACell.Style.Font.Size := 14;
  ACell.Style.Font.Style := [fsBold, fsUnderline];

The image below displays the worksheet after code execution.

Align Data Within a Cell

Use the AlignHorz and AlignVert properties of the TdxSpreadSheetCellStyle to align data within a cell. TdxSpreadSheetDataAlignHorz and TdxSpreadSheetDataAlignVert enumerators determine possible horizontal and vertical cell content alignments respectively.

The default AlignVert property setting is ssavGeneral, and the cell content is bottom-aligned. The horizontal alignment of the cell content is governed by the AlignHorz property with the ssahGeneral default format. ssahGeneral makes text, numbers, logical values aligned differently: text is aligned to the left, numbers are aligned to the right and logical values are centered within a cell.

When the cell content is edited, it is aligned left top regardless of alignment settings of the AlignHorz and AlignVert properties.

The following sample code sets different text alignments for the B2:G5 cell range.

var
  ACell: TdxSpreadSheetCell;
  HAlign: TdxSpreadSheetDataAlignHorz;
  VAlign: TdxSpreadSheetDataAlignVert;
  I, J: Integer;
//...
  I := 1;
  J := 1;
  for HAlign := ssahGeneral to ssahDistributed do
  begin
    for VAlign := ssavTop to ssavDistributed do
    begin
      ACell := dxSpreadSheet1.ActiveSheetAsTable.CreateCell(I, J);
      ACell.Style.AlignHorz := HAlign;
      ACell.Style.AlignVert := VAlign;
      ACell.Style.Font.Name := 'Tahoma';
      ACell.SetText('The quick brown fox jumps over the lazy dog');
      ACell.Style.WordWrap := True;
      ACell.Column.Size := 120;
      ACell.Row.Size := 120;
      I := I + 1;
    end;
  J := J + 1;
  I := 1;
  end;

The image below displays the worksheet appearance after code execution.

Apply Borders to a Cell

The ExpressSpreadSheet allows you to customize cell borders via the Borders property of the TcxSpreadSheetCellStyle object. This property returns TdxSpreadSheetCellBorder class instance. The individual cell borders can be accessed via the TcxBorder enumerator that has members for each of the cell edges. The TdxSpreadSheetCellBorder object has Color and Style properties that specify the TColor color and one of 15 predefined line styles respectively.

Use the following code to modify the border style of the B2 cell of the active worksheet.

var
  ACell: TdxSpreadSheetCell;
begin
  ACell := dxSpreadSheet1.ActiveSheetAsTable.CreateCell(1, 1);
  ACell.Style.Borders[bLeft].Color := clRed;
  ACell.Style.Borders[bLeft].Style := sscbsDouble;
  ACell.Style.Borders[bBottom].Color := clGreen;
  ACell.Style.Borders[bBottom].Style := sscbsMediumDashDotDot;
  ACell.Style.Borders[bRight].Color := clYellow;
  ACell.Style.Borders[bRight].Style := sscbsThick;
  ACell.Style.Borders[bTop].Color := clBlue;
  ACell.Style.Borders[bTop].Style := sscbsHair;
end;

The image below shows the worksheet appearance after code execution.

Apply Fill Pattern to a Cell

The ExpressSpreadSheet allows you to apply fill patterns to individual cells. Fill patterns are assigned and managed via the Brush property of the TdxSpreadSheetCellStyle object. This property returns a reference to the TdxSpreadSheetCellBrush object, which contains the Style, ForegroundColor and BackgroundColor properties.

The BackgroundColor and ForegroundColor properties specify the background color of the cell and the color used to fill it. The fill pattern is specified by the Style property of the TdxSpreadSheetCellFillStyle type, which is the enumerator containing 18 predefined fill patterns.

The following code example applies foreground and background colors to the cell as well as a fill pattern.

var
  ACell: TdxSpreadSheetCell;
//...
  ACell := dxSpreadSheet1.ActiveSheetAsTable.CreateCell(1, 1);
  ACell.Style.Brush.BackgroundColor := clBlue;
  ACell.Style.Brush.ForegroundColor := clRed;
  ACell.Style.Brush.Style := sscfsThinThickCrossHatch;

The image below displays the worksheet appearance after code execution.