I have a form with a query, a dataset, an editable dbgrid and an updatesql component. When I need to save the changes made in the dbgrid, I call this procedure:
procedure TEditCardDetailForm.SaveChanges;
begin
Database1.StartTransaction;
try
Query2.ApplyUpdates;
Database1.Commit;
except
Database1.Rollback;
raise;
end;
Query2.CommitUpdates;
end;
However I want the changes to be applied automatically to the database when I press Enter or go to another row after editing a cell in the dbgrid – the way it is done when I use a TTable component. Is there a way to do it?
You have two scenarios to handle here:
The first one is easy to implement by calling your
SaveChangesprocedure in theAfterPostevent of the underlying dataset (query2, aTClientDataSet?).For the second one you only have to call
query2.Postafter the column has changed. This can be done in theOnDataChangeevent of thedatasource. Make sure to check forField <> niland thedatasetbeing in insert or edit mode before callingpost.