[英]How to post data in TDBgrid immediately when exiting the cell being edited
I have a TDBGrid --> Tdatasource --> TQuery --> table in MySQL database.我在 MySQL 数据库中有一个 TDBGrid --> Tdatasource --> TQuery --> 表。
One field in the grid/query is Boolean and is shown as a checkbox.网格/查询中的一个字段是 Boolean 并显示为复选框。
I have written code to sort the grid by whichever column title I click following standard answers on SO.我已经编写了代码来按我单击的任何列标题对网格进行排序,遵循 SO 上的标准答案。
I have a 'Save' button that preforms quite a bit of unrelated processing but the first thing it does is我有一个“保存”按钮,它执行相当多的无关处理,但它做的第一件事是
MyQuery1.edit;
MyQuery1.Post;
to ensure that the last edit made is posted, even if the user doesn't move to another row before clicking 'save'.确保发布最后一次编辑,即使用户在单击“保存”之前没有移动到另一行。
The problem.问题。
If for example I tick one checkbox to change the field value from FALSE to TRUE and move to a different row then the data is posted back to the table as desired and the row stays ticked as I scroll例如,如果我勾选一个复选框以将字段值从 FALSE 更改为 TRUE 并移动到另一行,则数据会根据需要回发到表中,并且当我滚动时该行保持勾选状态
If I tick one checkbox and move off to click the 'save' button the code in that button does a post and the checkbox remains checked如果我勾选一个复选框并移动以单击“保存”按钮,则该按钮中的代码会发布一个帖子并且该复选框仍处于选中状态
However , If I tick one checkbox and move to the column header of a different column to sort the grid the data doesn't get posted and after the sort has completed the checkbox is unchecked但是,如果我勾选一个复选框并移动到另一列的 header 列以对网格进行排序,则不会发布数据,并且在排序完成后复选框未选中
The question问题
Is there some event I can use to do a post either immediately I tick (or untick) the checkbox or when I exit the column in which the checkbox resides.是否有一些事件可以用来发布帖子,或者我立即勾选(或取消勾选)复选框,或者当我退出复选框所在的列时。
What I have tried我试过的
I've tried the following events of the DBgrid but none seem to get called when I move off to click a column title我已经尝试了 DBgrid 的以下事件,但是当我离开单击列标题时似乎没有被调用
OnEditChange()
OnEditRecord(()
OnColExit(()
In case someone wants to see the full code of my 'unrelated processing' in the Save button it is this but, as I said, it is unrelated.如果有人想在“保存”按钮中查看我的“不相关处理”的完整代码,那就是这个,但正如我所说,它是不相关的。
procedure TFrmBulkSubdPaymentRecord.btnSaveClick(Sender: TObject);
var
i : Integer;
member_id_making_payment : Integer;
invoice_id : Integer;
cash_payment_ref : string;
payment_amount : Currency;
paid_date : TDateTime;
user_comment : string;
sql : string;
NunRecordSaved : integer;
SQL_of_changes : string;
begin
SQL_of_changes := '';
user_comment := 'Bulk insertion on ' + ReverseDateString(DateToStr(Date));
paid_date := Date;
NunRecordSaved := 0;
SMDBGrid1.DisableScroll;
MyQuery1.DisableControls; //to avoid dataset not in edit mode error see https://forums.devart.com/viewtopic.php?t=25580
MyQuery1.edit;
MyQuery1.Post;// make sure that even one row gets put in even if we don't move cursor off it
MyQuery1.EnableControls ;
MyQuery1.First ;
while not MyQuery1.eof do // if this row is ckecked, build up the paramenters and mark this invoice as paid
begin
if MyQuery1.FieldByName('payment_made').AsBoolean = true then
begin
member_id_making_payment := MyQuery1.FieldByName('member_id').asInteger;
invoice_id := MyQuery1.FieldByName('invoice_id').asInteger;
cash_payment_ref := MyQuery1.FieldByName('invoice_ref').asString;
payment_amount := MyQuery1.FieldByName('invoice_amount').AsCurrency ;
sql := 'CALL record_new_payment('
+ ToSQL.Int(member_id_making_payment) + ','
+ ToSQL.Int(invoice_id) + ','
+ ToSQL.MySQLText(cash_payment_ref) + ','
+ ToSQL.Float(payment_amount) +','
+ ToSQL.Date(paid_date) +','
+ ToSQL.MySQLText(user_comment) +');' ;
DMod.RunSQLCommand(sql);
inc(NunRecordSaved);
SQL_of_changes := SQL_of_changes +
U_SQLcode.SQL_ChangeMade(
member_id_making_payment, //AffectedMemberID integer
'New payment', //ChangeType
'Member: '+ inttostr(member_id_making_payment) + 'Invoice: ' + inttostr(invoice_id), //FieldChanged
'', //OldData
'£' + CurrToStr(payment_amount) , //Newdata
user_comment, //Comment
invoice_id //invoiceID integer
);
end;
MyQuery1.Next;
end;
SMDBGrid1.EnableScroll;
if NunRecordSaved > 0 then
begin
DMod.RunSQLCommand(SQL_of_changes);
ShowMessage(IntToStr(NunRecordSaved)+' payments for full subs amount saved' );
LoadGrid(TheFeeYear);
end
else
ShowMessage('No members ticked as having paid, nothng saved')
end;
I got round this by adding我通过添加解决了这个问题
MyQuery1.edit;
MyQuery1.Post;
at the start of the code that does the sorting.在进行排序的代码的开头。
But I'd still like to know if there is a better way to post when leaving a cell.但我仍然想知道在离开牢房时是否有更好的发帖方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.