Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Binding to Data
   [Expand]Grid View
   [Expand]View Technology
   [Expand]Grouping
   [Expand]Sorting
   [Collapse]Summaries
     Summaries Overview
     Tutorial: Custom Summary Functions
     Tutorial: Format Summary Text
     Tutorial: Group Summaries
     Tutorial: Obtain Summary Values
     Tutorial: Sort Group Rows by Summary Values
     Tutorial: Total Summaries
     Total Summary
     Group Summary
     Custom Aggregate Functions
     Format Summary Values
     Obtain Summary Values
     Sorting Group Rows by Summary Values
     Controlling Visibility of Group Summaries for Individual Group Rows
   [Expand]Data Editing
   [Expand]Filtering and Locating Rows
   [Expand]Focus and Selection Handling
   [Expand]Processing Rows
   [Expand]Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
   [Expand]Batch Modifications
   [Expand]Hit Information
   [Expand]Hints
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]Examples
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Tutorial: Custom Summary Functions

This walkthrough is a transcript of the Custom Summary Functions video available on the DevExpress YouTube Channel.

The DevExpress GridControl allows you to implement custom aggregate functions to calculate total and group summaries. In this tutorial, you will create a total summary that is calculated using values from two different fields. You will also implement a custom group summary to calculate the number of records that have Discontinued a field value set to true.

Expanded Starting Point

Start with a GridControl that doesn't have any summaries.

Expanded Adding a Total Summary

First, enable the View footer. Expand the GridView.OptionsView property and turn on the GridOptionsView.ShowFooter option.

After that, select the Unit Price column, expand its GridColumn.SummaryItem property and set GridSummaryItem.SummaryType to SummaryItemType.Custom. Customize text formatting using the GridSummaryItem.DisplayFormat property. Additionally, set the GridSummaryItem.Tag property to 1. This value will be used to identify this summary item in code.

Expanded Adding a Group Summary

Now create a new group summary. Invoke the Grid Designer, switch to the Group Summary Items Page and add a new item. Specify text formatting, set the GridSummaryItem.FieldName property to Discontinued, the GridSummaryItem.SummaryType property to SummaryItemType.Custom and the GridSummaryItem.Tag property to 2.

Expanded Implementing Custom Summary Calculation Algorithms

Go to the code view and declare two variables that will store summary values.

To implement the summary calculation algorithm, handle the View's GridView.CustomSummaryCalculate event. In the event handler, use the GridSummaryItem.Tag property to identify summary items.

  • Initialization

    To allow you to initialize summary start values, the GridView.CustomSummaryCalculate event is first raised with the CustomSummaryEventArgs.SummaryProcess parameter set to CustomSummaryProcess.Start. At this stage, you can reset the counter of discontinued products and the variable that will calculate the total price of all products in stock.

  • Calculation

    Then, the GridView.CustomSummaryCalculate event fires for each data row in the View or in a group with the CustomSummaryEventArgs.SummaryProcess parameter set to CustomSummaryProcess.Calculate. This is where you can accumulate your summary values. The currently processed row and field value are specified by the event's CustomSummaryEventArgs.RowHandle and CustomSummaryEventArgs.FieldValue parameters.

    For the total summary, you need to multiply a product's unit price by units in stock, and add the result to the previous total price. For the group summary, increase the counter provided that a product is discontinued.

  • Finalization

    The GridView.CustomSummaryCalculate event is raised one more time after processing all rows, and the CustomSummaryEventArgs.SummaryProcess parameter is set to CustomSummaryProcess.Finalize. At this stage, finalize the summary calculation and assign the summary value to the event's CustomSummaryEventArgs.TotalValue parameter.

Run the application to see the result. The View's footer cell under the Unit Price column displays the summary calculated against two fields at once. Group the grid's data by the Category column. Group rows display the number of discontinued products in each group.

Expanded Complete Code

Expanded See Also

How would you rate this topic?​​​​​​​