Example 1: Conditional formatting and a computed column

Uses OnBeforeDrawCell, the most flexible event on TEgDBGridEx. Two things in one handler: highlighting a value based on the data, and a column whose content does not come from a field at all.

Scenario

An orders grid with a Total field (can be negative for a refund) and a DueDate field. Two requirements: a negative Total should stand out in red and bold, and a Custom Cell column (“Status”) should show “Late” or “On time” computed from DueDate versus the current date - there is no such field in the dataset.

Setup

Add a TColumn for the Status column at design time with FieldName left empty - that is what makes it a Custom Cell column (Assigned(Column.Field) is False for it).

The handler

procedure TfrmOrders.GridBeforeDrawCell(Column: TColumn; var FontColor, BackColor: TColor;
  Font: TFont; var CellText: string; var ImageIndex: Integer; var ImageAlignment: TAlignment);
begin
  // Column.Field is nil for the Status column (empty FieldName) - always
  // check before reading it, unlike a normal bound column.
  if Column.FieldName = 'Total' then
  begin
    if Table.FieldByName('Total').AsFloat < 0 then
    begin
      FontColor := clRed;
      Font.Style := Font.Style + [fsBold];
    end;
  end
  else if Column.FieldName = 'Status' then
  begin
    if Table.FieldByName('DueDate').AsDateTime < Date then
      CellText := 'Late'
    else
      CellText := 'On time';
  end;
end;

Why it works this way

Also available per column

The same signature is available as TEgColumn.OnBeforeDrawCell, firing after the grid-level handler (if both are assigned). Useful to keep a column’s own formatting logic attached to that column instead of a single large grid-wide handler with a long if/else chain by FieldName.

See Also

(C) 2026 Easygate, Lda