[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [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
   [Collapse]Get Started With Data Grid and Views
     [Expand]Appearance and Conditional Formatting
     [Expand]Data Editing
     [Expand]Hit Information
     [Expand]Data Binding and Working with Columns
     [Expand]Grid View - Columns, Rows and Cells
     [Expand]Banded Views
     [Expand]WinExplorer View
     [Expand]Tile View
     [Expand]Filter and Search
       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
     [Expand]Split Presentation
     [Expand]Row Preview Sections
   [Expand]Data Binding
    Unbound Columns
   [Expand]Data Editing and Validation
   [Expand]Filter and Search
   [Expand]Focus and Selection Handling
    Format Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
   [Expand]Batch Modifications
    Hit Information
    Popup Menus
   [Expand]Save and Restore Layout
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Gantt Control
  [Expand]Chart Control
  [Expand]Map Control
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

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

Is this topic helpful?​​​​​​​