Uses OnCheckChange,
which fires before a check box changes, to refuse a change that breaks a
rule and to update a related field when the change is accepted.
The boolean field Active is shown as a check box. A
product that still has stock cannot be made inactive; when the box does
change, the field UpdatedAt records the moment.
Besides EgFireGrid: System.SysUtils
(SameText, Abort, Now),
FMX.DialogService (TDialogService) and the
unit of the dataset component (here FireDAC.Comp.Client,
for Query1).
procedure TForm1.Grid1CheckChange(Sender: TObject; Column: TEgFireGridColumn; RowIndex: Integer);
var
WasActive: Boolean;
begin
if not SameText(Column.FieldName, 'Active') then
Exit;
WasActive := Query1.FieldByName('Active').AsBoolean;
if WasActive and (Query1.FieldByName('Stock').AsInteger > 0) then
begin
TDialogService.ShowMessage('A product with stock cannot be made inactive.');
Abort;
end;
Query1.Edit;
Query1.FieldByName('UpdatedAt').AsDateTime := Now;
end;
Active
still holds the old value, and the grid writes the opposite one after
the handler.Abort refuses the change: the
exception is silent, and the grid does not toggle the value.OnChange and call
Post there instead of editing in this event.