I have a grid with a few columns that have SummarySettings attached. In addition to showing the row summaries as a footer for the entire list or the current groupby's list, I want to make it so if you select a bunch of rows, it calculates the summaries for the selection. (Excel does something similar in its status bar if you select some cells.)
However, most of the classes like SummaryValue as well as methods like ICustomSummaryCalculator.BeginCustomSummary seem to rely on a RowsCollection, and it is apparently not possible to create a custom RowsCollection instance. I really just want to pass a subset of rows (currently an IEnumerable<UltraGridRow>) and have SummaryValue perform aggregations on that.
Is there a trick I'm missing?
Hello Sören, The ICustomSummaryCalculator interface has three methods to be implemented.
BeginCustomSummary begins the summary for the SummarySettings object passed in. The implementation of this method should reset any state variables used for calculating the summary.
EndCustomSummary gets called when every row has been processed, so here is where the calculated summary value will be returned.
AggregateCustomSummary - here is where the actual calculations are done and each row that gets passed in is being processed.
Attached is a sample project where I have created a custom summary that sums the values of the cells in the ‘Price’ column of the grid’s selected rows only. In BeginCustomSummary I set the total to zero, in the AggregateCustomSummary method I check if the row is selected and add it to the total sum and at the end in EndCustomSummary method I return the total sum.
As there is no way for the grid to know that the summary is based on selection and it does not automatically recalculate the summaries when the selection changes, we have to handle the AfterSelectChange event and tell it when the summaries need to be recalculated.
Please let me know if you need further assistance regarding this matter.
thanks for your reply. I'm not trying to customize how a cell is summarized, but rather which rows are summarized, so I don't think implementing ICustomSummaryCalculator is appropriate here..
As far as I can tell, the grid offers the following two row collections for summaries: A) the entire list (with row filters applied), or B) the current group within a groupbyrow. I'm trying to implement a third variant C) a given custom subset of rows.
In the AggregateCustomSummary method, you can also filter the RowsCollection (check if the rows from the RowCollection meet your conditions). In the sample, I sent you I check if the row is selected and if so, I add its cell value to the sum.
To add a custom summary to the summary dialog you can handle the grid’s BeforeSummaryDialog event, where you can modify the control layout and add additional checkboxes to the SummaryDialog form.
In the sample project, I have added some code to demonstrate this approach. In the BeforeSummaryDialog event handler, I remove the default Sum summary control from the ‘Price’ column and replace it with a new ‘Custom summary’, which sums up only the grid’s selected rows.