Search code examples
delphiindexingaggregatetclientdataset

TAggregateField not calculated when Index of ClientDataset changes


I am using a TClientDataset, connected to a DBGrid, with a couple of Aggregate Fields, for calculating the SUM of an other couple of Float Fields. All fields have been created in Design time.

Everything is working as expected, until the time that the IndexName of the ClientDataset changes with a custom Index, in order to sort the Grid. After that, the Aggregate Fields don't calculate their value properly, and they are set with Null value.

The problem occurs in Delphi XE7.


Solution

  • I have google about it and i found a solution that worked for me here

    There is a bug at TCustomClientDataSet.SetIndex method that is declared in the DBClient unit. The solution propose to replace the following code

    if FAggregatesActive then
    begin
        FAggFieldsInit := False;
        ResetAllAggs(FAggregatesActive);
    

    with the next one

    if FAggregatesActive then
    begin
        CloseAggs;
        ResetAllAggs(FAggregatesActive);
    

    As far as i understand, the replacement of FAggFieldsInit := False with CloseAggs, force the Aggregates to be released and then recalculate with the new index. As i said this solution worked for me, and i haven't notice any unwanted behavior.

    The credit goes to AndreyZ for the original solution.