Calculating Fields

Just like lookup fields, calculated fields are virtual fields which you can add to a Table object. A calculated field differs from a lookup field in that rather than look up its value from another table based on a key, it will calculate its value using code. To demonstrate this, you will now use a calculated field to improve how you display data for the Done column, which currently shows its value as integer (0, 1).

Setting up the calculated field

Calculated fields in Data Abstract datasets work exactly the same way as they do in any other dataset. You will need to add your calculated field to the fields of the tbl_Tasks component on the DataModule.

Data Module - Add Calculated Field

Now set its value in the dataset's CalcFields event.

procedure TClientDataModule.tbl_TasksCalcFields(DataTable: TDADataTable);
begin
  if DataTable.FieldByName('Done').AsInteger = 0 then
    DataTable.FieldByName('IsDone').AsWideString := 'Open'
  else
    DataTable.FieldByName('IsDone').AsWideString := 'Done';
end;

Using the calculated field

Next reassign the binding for the Done column in your DBGrid to point to the isDone field, and your grid should now look like this:

Calculated Field in Action