Jørn E. Angeltveit
Jørn E. Angeltveit

Reputation: 3079

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?

Upvotes: 4

Views: 9361

Answers (3)

Ivan Ko
Ivan Ko

Reputation: 9

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)

Upvotes: 0

Andrew Rademacher
Andrew Rademacher

Reputation: 345

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.

Upvotes: 2

Sertac Akyuz
Sertac Akyuz

Reputation: 54812

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.

Upvotes: 6

Related Questions