Search code examples
delphifiremonkeytstringgridlivebindingsrad-studio

How to post data to database immediately after editing is done in a firemonkey TStringGrid


In Rad Studio 10.3, I connect a StringGrid to a database by using LiveBindins Wizard and selecting FireDAC. Everything is fine except below issue:

When user edits a cell and presses Enter the value is edited correctly but the new value does not post to database until user navigates to another row. In other words, if user changes a cell's value and stays at current row the new value is not posted to database.

Is there any way to post the new value immediately after editing is done? If yes, how?

If a sample is required here is the link of my issue's sample project.


Solution

  • the new value is not posted to database!

    One reason it isn't is to allow the user to change their mind and cancel the change. Changes in a database often involve knock-on consequences activated e.g. by server-side triggers to make changes/additions/deletions to other tables to maintain the consistency of the data.

    So, you need to call the dataset's Post method in order to save the change to the database, preferably after offering the user the opportunity to confirm or cancel the change. The TBindNavigator is an often-used, non-intrusive way to do this and includes Save and Cancel buttons which light up as soon as a change to any field in the dataset is made, so it avoids having to confront the user with a pop-up dialog asking whether the change should be made or cancelled.

    If you want to avoid using a TBindNavigator, you could set up an event handler on the StringGrid like this:

    procedure TForm2.StringGrid1EditingDone(Sender: TObject; const ACol,
      ARow: Integer);
    begin
      if DataSet.State in [dsEdit, dsInsert] then
        DataSet.Post;
    end;