简体   繁体   English

如何在Firemonkey TStringGrid中完成编辑后立即将数据发布到数据库

[英]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. 在Rad Studio 10.3中,我使用LiveBindins向导并选择FireDAC将StringGrid连接到数据库。 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. 当用户编辑单元格并按Enter时,将正确编辑该值,但新值将不会发布到数据库,直到用户导航到另一行。 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. 如果需要样本, 这是我的问题的样本项目的链接

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 eg 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. 因此,您需要调用数据集的Post方法以便将更改保存到数据库,最好是在为用户提供确认或取消更改的机会之后。 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. TBindNavigator是一种常用的非侵入式方法,它包括“ Save和“ Cancel按钮,一旦对数据集中的任何字段进行更改,它们都会亮起,因此避免了使用弹出式菜单面对用户的情况。对话框,询问是否应进行更改或取消更改。

If you want to avoid using a TBindNavigator, you could set up an event handler on the StringGrid like this: 如果要避免使用TBindNavigator,可以在StringGrid上设置事件处理程序,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM