Q:

Form Running Totals, Ax 2009

Is there an example anywhere of a form that performs running totals in a column located within a grid.  The user ordering and filtering of the grid would affect the running totals column.
I can easily perform the above if it was ordering only by transaction date, but including the user ordering and filtering I presume that we would have to use the datasource range() and rangecount() functions (see SysQuery::mergeRanges() for an example) then iterate over these to apply the filtering, then include the dynalinks.  The same for the ordering, albeit this is now more complicated.
Any suggestions appreciated.  Any appreciations suggested (as in: vote the question up!).

A:

You could implement it as a form datasource display method using this strategy:

Copy the form's datasource query (no need for SysQuery::mergeRanges):
QueryRun qr = new QueryRun(ledgerTrans_qr.query());
Iterate and sum over your records using qr, stop after the current record:
while (qr.next())
{
    lt = qr.getNo(1);
    total += lt.AmountMST;
    if (lt.RecId == _lt.RecId)
        break;
}
This could be made more performant if the sorting order was fixed (using sum(AmountMST) and adding a where constraint).
Return the total

This is of cause very inefficient (subquadratic time, O(n^2)).
Caching the results (in a map) may make it usable if there are not too many records.
Update: a working example.

