Custom Summaries
- 2 minutes to read
Custom Summaries
The Tree List provides a TreeList.GetCustomSummaryValue event which enables you to implement custom aggregate functions. For instance, you may want to calculate the number of nodes which have column values below a certain level. Another example is calculating a discount from the maximum value. In such cases, you should write a GetCustomSummaryValue event handler.
If you want to calculate the footer summary or group summary in a custom way, set the TreeListColumn.SummaryFooter or TreeListColumn.RowFooterSummary property to SummaryItemType.Custom. Otherwise the TreeList.GetCustomSummaryValue event will not fire.
The event’s parameter provides the following properties that can be used to calculate the resulting summary value.
GetCustomSummaryValueEventArgs.Nodes
Contains a collection of nodes involved in calculations.
GetCustomSummaryValueEventArgs.Column
Gets the column whose values are used to calculate the summary value.
GetCustomSummaryValueEventArgs.IsSummaryFooter
Returns true if the footer summary value is being calculated. false is returned if the group summary value is being calculated.
GetCustomSummaryValueEventArgs.CustomValue
Contains the summary result. Write the calculated value to this property.
Example
The following example calculates the number of records for which the “Budget” column’s value exceeds 500,000. The setCustomSummary method sets the custom summary type for the Budget column. The TreeList.GetCustomSummaryValue event handler implements the calculation logic.
The image below shows the result.
private void setCustomSummary() {
treeList1.OptionsView.ShowRowFooterSummary = true;
treeList1.Columns["Budget"].RowFooterSummary = SummaryItemType.Custom;
treeList1.Columns["Budget"].RowFooterSummaryStrFormat = "{0} nodes exceed limit";
}
private void treeList1_GetCustomSummaryValue(object sender, DevExpress.XtraTreeList.GetCustomSummaryValueEventArgs e) {
if(e.Column.FieldName == "Budget") {
IEnumerator en = e.Nodes.GetEnumerator();
int exceedingLimitNodes = 0;
while(en.MoveNext()) {
TreeListNode node = (TreeListNode)en.Current;
decimal budget = (decimal)node.GetValue(e.Column);
if(budget > 500000) {
exceedingLimitNodes ++;
//...
}
}
e.CustomValue = exceedingLimitNodes;
}
}