torsdag den 11. september 2008

Sum grid records

Normal way to make totals in a form is to create display methods, preferably on the table, where it will sum all records in the table. But if you only want to sum the records shown in the grids, bearing in mind that the grid can have several filters, it becomes a little trickier.

What you must do is create a new method, that you transfer the datasource of the grid into.


Example:

In this example I want to sum the AmountMST in a CustTrans grid

AmountMST calcSum(FormDataSource _ds)
{
Query query;
QueryRun qr;
QueryBuildDataSource qbds;

CustTrans custTrans;
AmountMST sumAmountMST;
;
query = new Query();
SysQuery::mergeRanges(query, _ds.queryRun().query(), true, true);

qbds = query.dataSourceTable(tablenum(CustTrans));
qbds.addSelectionField(fieldnum(CustTrans, AmountMST), SelectionField::Sum);

qr = new QueryRun(query);

while (qr.next() )
{
custTrans = qr.get(tablenum(CustTrans));
sumAmountMST = custTrans.AmountMST;
}

return sumAmountMST;
}

Ingen kommentarer: