Example 5: Values that are not fields

Uses <var> markers and OnGetVariableValue to put in titles and cells values that are not fields of the dataset, and VariableRowIndex to compute a value for the row being drawn.

Scenario

Setup

// FormCreate
Grid1.ColumnByFieldName('Price').Title.Caption := 'Price<br>(<var>Currency</var>)';
Grid1.HeaderHeight := 40;   // room for the second line of the title
Grid1.RowHeight := 36;      // room for two lines in the cells

Units

Besides EgFireGrid: System.SysUtils (SameText, IntToStr, FormatSettings, Date) and System.Variants (VarIsNull, VarToDateTime).

The handlers

procedure TForm1.Grid1BeforeDrawCell(Sender: TObject; Column: TEgFireGridColumn; RowIndex: Integer;
  Cell: TEgFireGridCellControl; var Text: string);
begin
  if SameText(Column.FieldName, 'Name') then
    // Both names are fields: the grid replaces them with the fields' text in this row
    Text := '<var>Name</var><br><var>Category</var>'
  else if SameText(Column.Title.Caption, 'Updated') then
    Text := '<var>DaysSinceUpdate</var> days';
end;

procedure TForm1.Grid1GetVariableValue(Sender: TObject; const VarID: string; var Value: string);
var
  UpdatedAt: Variant;
begin
  if SameText(VarID, 'Currency') then
    Value := FormatSettings.CurrencyString
  else if SameText(VarID, 'DaysSinceUpdate') and (Grid1.VariableRowIndex >= 0) then
  begin
    UpdatedAt := Grid1.RowSource.GetValue(Grid1.VariableRowIndex, 'UpdatedAt');
    if not VarIsNull(UpdatedAt) then
      Value := IntToStr(Trunc(Date - VarToDateTime(UpdatedAt)));
  end;
end;

Why it works this way

See Also

(C) 2026 Easygate, Lda