Back to Devexpress

Custom Summaries

windowsforms-5611-controls-and-libraries-tree-list-feature-center-summaries-custom-summaries.md

latest3.9 KB
Original Source

Custom Summaries

  • Nov 13, 2018
  • 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.

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.

csharp
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;
   }
}
vb
Private Sub setCustomSummary()
   TreeList1.OptionsView.ShowRowFooterSummary = True
   TreeList1.Columns("Budget").RowFooterSummary = SummaryItemType.Custom
   TreeList1.Columns("Budget").RowFooterSummaryStrFormat = "{0} nodes exceed limit"
End Sub

Private Sub TreeList1_GetCustomSummaryValue(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.GetCustomSummaryValueEventArgs) _
Handles TreeList1.GetCustomSummaryValue
   If (e.Column.FieldName = "Budget") Then
      Dim en As IEnumerator = e.Nodes.GetEnumerator()
      Dim exceedingLimitNodes As Integer = 0
      Dim node As TreeListNode
      While en.MoveNext() = True
         node = CType(en.Current, TreeListNode)
         Dim budget As Decimal = CType(node.GetValue(e.Column), Decimal)
         If (budget > 500000) Then
            exceedingLimitNodes += 1
            '...
         End If
      End While
      e.CustomValue = exceedingLimitNodes
   End If
End Sub