Skip to main content
Table of Contents

TTMSFNCDataGrid.OnGetCellClass Event

Occurs when the grid needs the cell item class for a cell.

API unit family: TMSFNCDataGrid Declaring type: TTMSFNCDataGrid Inherited from: TTMSFNCCustomDataGrid Type: TTMSFNCDataGridGetCellClassEvent

Syntax

FMX.TMSFNCDataGrid.hppPascalC++
property OnGetCellClass: TTMSFNCDataGridGetCellClassEvent;
__property TTMSFNCDataGridGetCellClassEvent OnGetCellClass = {read=GetOnGetCellClass, write=SetOnGetCellClass};

Usage examples

Provide a custom cell class for specific rows or columns

Subclass TTMSFNCDataGridCell, override Draw / DrawText / etc., and let the grid instantiate your class via OnGetCellClass.

Provide a custom cell class for specific rows or columnsPascalC++
type
  TStarRatingCell = class(TTMSFNCDataGridCell)
  protected
    procedure DrawText(AGraphics: TTMSFNCGraphics; ARect: TRectF;
      AText: string); override;
  end;

procedure TStarRatingCell.DrawText(AGraphics: TTMSFNCGraphics; ARect: TRectF;
  AText: string);
var
  Stars: Integer;
  Display: string;
begin
  // Render the integer value as that many stars
  if TryStrToInt(AText, Stars) then
    Display := StringOfChar('*', Max(0, Min(5, Stars)))
  else
    Display := AText;
  inherited DrawText(AGraphics, ARect, Display);
end;

procedure TForm1.GridGetCellClass(Sender: TObject; AColumn, ARow: Integer;
  var ACellClass: TTMSFNCDataGridCellClass);
begin
  if AColumn = 4 then               // the rating column
    ACellClass := TStarRatingCell;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Grid.OnGetCellClass := GridGetCellClass;
  Grid.Cells[4, 1] := 5;
  Grid.Cells[4, 2] := 3;
  Grid.Cells[4, 3] := 4;
end;
// The Pascal snippet overrides DrawText(AGraphics, ARect, AText), which the cell class does not declare;
// the real virtual is DrawText(AGraphics), so the text is drawn from the override.
class TStarRatingCell : public TTMSFNCDataGridCell
{
protected:
  virtual void __fastcall DrawText(TTMSFNCGraphics *AGraphics);
};

void __fastcall TStarRatingCell::DrawText(TTMSFNCGraphics *AGraphics)
{
  // Render the integer value as that many stars
  int Stars;
  System::UnicodeString Display;
  if (TryStrToInt(Text, Stars))
    Display = StringOfChar(L'*', Max(0, Min(5, Stars)));
  else
    Display = Text;
  AGraphics->Font->Assign(Layout->Font);
  AGraphics->DrawText(Rect, Display);
}

void __fastcall TForm1::GridGetCellClass(TObject *Sender, int AColumn, int ARow,
  TTMSFNCDataGridCellClass &ACellClass)
{
  if (AColumn == 4)               // the rating column
    ACellClass = __classid(TStarRatingCell);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Grid->OnGetCellClass = GridGetCellClass;
  Grid->Ints[4][1] = 5;
  Grid->Ints[4][2] = 3;
  Grid->Ints[4][3] = 4;
}

Read the related guide