What’s the best way to calculate aggregate fields in TDAMemDataTables?

Scenario I have, which must be a fairly common one, is a parent table with a collection of items in a detail table and I need to total the detail fields. In my case it’s an invoice with detail lines. I need to show an interface where the user can add/edit/delete detail lines to the invoice in the local TDAMemDataTable and I obviously need to show the current total cost and price at the bottom.

I’m very surprised there’s no in-built aggregate system in the component, as there is for Delphi’s own TClientDataSet.

I guess I’m going to have to try to implement it myself but wondered if anyone had any ideas on the best way to do it. Trapping loads of events on the two tables and then iterating the items to sum the figures seems clunky and prone to problems.


you can calculate initial values and later recalculate value only if detail lines were changed.
also you can use cloned table feature for filtering records of detail table