Back to Devexpress

TdxSpreadSheetCell Class

vcl-dxspreadsheetcore-71cbe5b6.md

latest12.6 KB
Original Source

TdxSpreadSheetCell Class

Stores cell content and custom style settings in a worksheet.

Declaration

delphi
TdxSpreadSheetCell = class(
    TdxDynamicListItem,
    IdxSpreadSheetCellData,
    IdxSpreadSheetCellStyleOwner
)

Remarks

Spreadsheet and Report Designer controls create TdxSpreadSheetCell class instances on demand to store and manage cell content and custom style settings. A spreadsheet control creates a TdxSpreadSheetCell class instance every time a user:

  • Assigns a value to an empty cell directly or through the associated TdxSpreadSheetFormulaBar control
  • Customizes an empty cell’s style settings

You can create TdxSpreadSheetCell class instances to populate cells in code as demonstrated in the code example below.

Main API Members

The list below outlines key members of the TdxSpreadSheetCell class. These members allow you to manage cell content and configure the appearance of individual cells.

Value Management API Members

AsBoolean | AsCurrency | AsDateTime | AsFloat | AsInteger | AsString | AsVariantAllow you to assign a value of different types to the cell or cast its value to the required type.AsFormula

Provides access to the parsed formula expression in the cell.

This property returns nil (in Delphi) or nullptr (in C++Builder) if the cell has no parsed formula expression. You can use the DataType or IsFormula property to identify if the cell contains a parsed formula expression.

ClearClears the cell value.DataTypeReturns the current cell data type.GetAsRTFReturns cell content as an RTF string.HasValueAllows you to identify if the cell has a value.IsEmptyIdentifies if the cell is empty.IsFormula

Identifies if the cell contains a parsed formula expression.

If this property returns True, you can use the AsFormula property to access the parsed formula expression.

IsNumericValueIdentifies if the cell stores a numeric value of any type.SetAsRTFAllows you to assign a formatted RTF string to the cell.SetTextAllows you to assign an unformatted text string or a formula expression to the cell.

Worksheet-Related API Members

Column | RowSpecify the cell’s parent column and row. You can use these properties to move the cell or identify its position in the parent worksheet.ColumnIndex | RowIndexSpecify indexes of the cell’s parent column and row. You can use these properties to move the cell or identify its position in the parent worksheet.GetReferenceReturns a reference to the cell. You can use this reference to address the cell in a formula expression or defined name.IsMergedAllows you to identify if the cell belongs to a merged cell range.ViewProvides access to the parent worksheet.

Appearance and Behavior Settings

ShowFormulaSpecifies if the cell shows the source formula expression string or a calculated result if the cell contains a parsed formula expression.StyleProvides access to all cell style settings. Individual style settings accessible through this property have priority over corresponding global cell style settings.

General-Purpose API Members

AssignCopies content and style settings between cell objects.AsError

Returns the cell’s error code.

This property value differs from ecNone only if an error occurred during formula expression parsing or evaluation.

DisplayTextReturns the cell’s display text string.GetAbsoluteBoundsReturns the cell’s bounding rectangle (in pixel coordinates).SpreadSheetProvides access to the parent Spreadsheet or Report Designer control.

Code Example: Populate Cells and Customize Cell Appearance

The following code example performs two batch operations at control and worksheet levels to change the default (document-wide) cell style, populate cells in the active worksheet, and apply a different style to the populated cells:

delphi
var
  ATableView: TdxSpreadSheetTableView;
  ACell: TdxSpreadSheetCell;
  I, J: Integer;
begin
  ATableView := dxSpreadSheet1.ActiveSheetAsTable; // Accesses the active worksheet

  // Change the spreadsheet document-wide cell style (the first batch operation)
  dxSpreadSheet1.BeginUpdate; // Initiates the following batch change at the control level
  try
    dxSpreadSheet1.DefaultCellStyle.Brush.BackgroundColor := clWebLightBlue;
    dxSpreadSheet1.DefaultCellStyle.Brush.ForegroundColor := clWebLightGreen;
    dxSpreadSheet1.DefaultCellStyle.Brush.Style := sscfsDiagonalStrip;
    dxSpreadSheet1.DefaultCellStyle.Borders[bRight].Color := clLtGray;
    dxSpreadSheet1.DefaultCellStyle.Borders[bRight].Style := sscbsThin;
    dxSpreadSheet1.DefaultCellStyle.Borders[bBottom].Color := clLtGray;
    dxSpreadSheet1.DefaultCellStyle.Borders[bBottom].Style := sscbsThin;
  finally
    dxSpreadSheet1.EndUpdate; // Calls EndUpdate regardless of the batch operation's success
  end;

  // Create and populate 75 cells with random integer values (the second batch operation)
  Randomize; // Initializes the random number generator
  ATableView.BeginUpdate; // Initiates the following batch change at the worksheet level
  try
    for I := 0 to 14 do // Iterates through 15 rows
      for J := 0 to 4 do // Iterates through 5 columns
      begin
        ACell := ATableView.CreateCell(I, J); // Creates a cell object
        ACell.AsInteger := Random(1000); // Populates the created cell object with a random integer value
        // Customize cell style settings
        ACell.Style.Borders[bLeft].Color := clWhite;
        ACell.Style.Borders[bLeft].Style := sscbsDotted;
        ACell.Style.Borders[bBottom].Color := clWhite;
        ACell.Style.Borders[bBottom].Style := sscbsDotted;
        ACell.Style.Font.Style := [fsBold, fsItalic];
        ACell.Style.Font.Color := clWhite;
        ACell.Style.Brush.Style := sscfsRevDiagonalStrip;
        ACell.Style.Brush.BackgroundColor := clLime;
        ACell.Style.Brush.ForegroundColor := clGreen;
      end;
  finally
    ATableView.EndUpdate; // Calls EndUpdate regardless of the batch operation's success
  end;
end;
cpp
TdxSpreadSheetTableView *ATableView;
  TdxSpreadSheetCell *ACell;
  // ...
  ATableView = dxSpreadSheet1->ActiveSheetAsTable; // Accesses the active worksheet

  // Change the spreadsheet document-wise cell style (the first batch operation)
  dxSpreadSheet1->BeginUpdate(); // Initiates the following batch change at the control level
  try
  {
    dxSpreadSheet1->DefaultCellStyle->Brush->BackgroundColor = clWebLightBlue;
    dxSpreadSheet1->DefaultCellStyle->Brush->ForegroundColor = clWebLightGreen;
    dxSpreadSheet1->DefaultCellStyle->Brush->Style = sscfsDiagonalStrip;
    dxSpreadSheet1->DefaultCellStyle->Borders[bRight]->Color = clLtGray;
    dxSpreadSheet1->DefaultCellStyle->Borders[bRight]->Style = sscbsThin;
    dxSpreadSheet1->DefaultCellStyle->Borders[bBottom]->Color = clLtGray;
    dxSpreadSheet1->DefaultCellStyle->Borders[bBottom]->Style = sscbsThin;
  }
  __finally
  {
    dxSpreadSheet1->EndUpdate(); // Calls EndUpdate regardless of the batch operation's success
  }

  // Create and populate 75 cells with random integer values (the second batch operation)
  Randomize(); // Initializes the random number generator
  ATableView->BeginUpdate();
  try
  {
    for(int i = 0; i < 15; i++) // Iterates through 15 rows
      for(int j = 0; j < 5; j++) // Iterates through 5 columns
      {
        ACell = ATableView->CreateCell(i, j); // Creates a cell object
        ACell->AsInteger = Random(1000); // Populates the created cell object with a random integer value
        // Customize cell style settings
        ACell->Style->Borders[bLeft]->Color = clWhite;
        ACell->Style->Borders[bLeft]->Style = sscbsDotted;
        ACell->Style->Borders[bBottom]->Color = clWhite;
        ACell->Style->Borders[bBottom]->Style = sscbsDotted;
        ACell->Style->Font->Style = TFontStyles() << fsBold << fsItalic;
        ACell->Style->Font->Color = clWhite;
        ACell->Style->Brush->Style = sscfsRevDiagonalStrip;
        ACell->Style->Brush->BackgroundColor = clLime;
        ACell->Style->Brush->ForegroundColor = clGreen;
      }
  }
  __finally
  {
    ATableView->EndUpdate(); // Calls EndUpdate regardless of the batch operation's success
  }

Cell Object Deletion

You can call the following procedures to delete TdxSpreadSheetCell class instances:

TdxSpreadSheetTableView.DeleteAllCellsRemoves all cell objects from the worksheet.TdxSpreadSheetTableView.DeleteCells

Direct TdxSpreadSheetCell Class References

The following public API members reference a TdxSpreadSheetCell object:

TdxSpreadSheetTableView.CellsProvides indexed access to cell objects in the worksheet.TdxSpreadSheetTableItem.CellsProvides zero-based indexed access to the cell objects hosted within the current table item object.

Implements

IdxSpreadSheetCellData

Inheritance

TObject TcxIUnknownObject TdxDynamicListItem TdxSpreadSheetCell

See Also

TdxSpreadSheetCell Members

dxSpreadSheetCore Unit