delphitclientdataset

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


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 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?


Solution

  • 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.