To optimize performance, I built a custom component that allows the user to choose which aggregates should be calculated. This component ensures that <GridAggregate> tags are inserted into <GridAggregates> via a foreach loop.
This works without any issues, at least when the selection is made for the first time.
However, if a selection is made in which the column of a new aggregate appears before an already selected aggregate, errors occur in the grid. AggregateResults now contains duplicates of an existing aggregate, and the new aggregates are missing.
Test project available in ticket 1718830.