开发者

How is it possible to manually execute the "OnCalcFields" event?

开发者 https://www.devze.com 2023-03-08 07:31 出处:网络
Say that I temporarily want to disable the OnCalcFields event (eg. by setting cdsCalcFields := nil) during a time-consuming operation on a TClientDataSet.How can I tell the TClientDataSet to perform a

Say that I temporarily want to disable the OnCalcFields event (eg. by setting cdsCalcFields := nil) during a time-consuming operation on a TClientDataSet. How can I tell the TClientDataSet to perform a recalculation of the calculated fields when I re-attach the OnCalcFields method?

Another situation that might require a manual recalculation is the situation where 开发者_C百科some of the calculated fields are depending on other datasets (eg. a calculated field is used to temporarily hold some aggregated value from the other dataset). This would work fine in most cases because the OnCalcFields events are executed often enough to get the correct value from the other dataset. But in some circumstances a recalculation is necessary to obtain the correct value from the other dataset.

Setting the AutoCalcFields property to False might also get you into some situation where a manual recalculation is desired.

I've seen several explanations on how to reduce the execution of the OnCalcFields event, but I can't find a simple way to just perform a recalculation...

Any suggestions?


Calculated fields are calculated when records are retrieved from the database, so call Refresh (or Close -> Open) on the dataset to force a re-calculation.

(Regarding the comments on the question), to force a re-calculation on only one record you can call RefreshRecord on the dataset. If the particular dataset descendant does not implement the method, an Edit followed by a Cancel call would achieve the same.


Calling Refresh or Close-> can cause the entire table to reload from the database. If this isn't something you want you can just call the OnCalc method your self passing it the CDS. Though you may have to slide the cursor manually.

with DisplayAcctListCDS do begin
  First;
  while not Eof do begin
    Edit;
    DisplayAcctListCDSCalcFields(DisplayAcctListCDS);
    Next;
  end;
end;

Assuming DisplayAcctListCDS is your TClientDataSet with calculated fields and DisplayAcctListCDSCalcFields is the generated event method for OnCalcFields.


This is a bit of a hack, but 100% of answer on this qestion for me!

DBGrid.Height := 30; 
DBGrid.Height := 200; // Refresh all Rows after first
CalculatedProc(DataSet); // Refresh first calculated fields. (Write name of your calculate procedure)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号