2
votes

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?

2
Can you please specify which Delphi version are you using? Also, I'd suggest to avoid the use of BDE components if possible, since they are long deprecated. - Guillem Vicens
So what ? D5 is more or less capable of making EXEs compatible to recent Windows. Even XP Themes avail from SoftGems. But BDE has rather poor compatibility report with newer Windows and no more development or support. In my last D5 days i used Firebird database back-end with UIB component set - Arioch 'The

2 Answers

2
votes

If I understand it right (please correct me if not) you have a TQuery with CachedUpdates set to true, but want it to behave as if it would not be using cached but immediate updates. If that is the case the way you have set up your TQuery contradicts your desired behaviour. Cached updates are to be "held" on the client side until you decide to manually post them to the database by using ApplyUpdates.

In case you can set CachedUpdates to false, you only need to do following:

  1. Link the TUpdateSQL to the TQuery via its UpdateObject property.
  2. Write the insert, update and delete statements and assign them to the InsertSQL, ModifySQL and DeleteSQL properties of the TUpdateSQL.

I guess you already have done these two things, so putting CachedUpdates to false should do it.

You can find more information on Cached Updates for Delphi 5 here.

HTH

1
votes

You have two scenarios to handle here:

  1. save changes when changing the grid row
  2. save changes when changing the grid column

The first one is easy to implement by calling your SaveChanges procedure in the AfterPost event of the underlying dataset (query2, a TClientDataSet?).

For the second one you only have to call query2.Post after the column has changed. This can be done in the OnDataChange event of the datasource. Make sure to check for Field <> nil and the dataset being in insert or edit mode before calling post.