Skip to main content
A newer version of this page is available. .

Calculate a Custom Summary

  • 3 minutes to read

This tutorial describes the steps required to calculate a custom summary that is not one of the built-in summary functions.

Note

This approach to calculating summaries applies when the UserDesignerOptions.DataBindingMode is set to DataBindingMode.Bindings.

See Calculate an Advanced Summary to learn about the alternative approach to calculating custom summaries without handling control events.

See Data Binding Modes to learn more about the available binding modes.

The report created in this tutorial displays the total number of packs of product units in a group:

How to -  CalculateCustomSum2

Do the following to calculate a custom summary in a report:

  1. To create a table report in this tutorial, start with a report that is bound to the “Products” table of the sample Northwind database (the nwind.mdb file included in the XtraReports installation). To learn more about binding a report to a data source, see Provide Data to Reports. In this tutorial, we will start with the following report layout.

    How to - CalculateCustomSum_0

  2. To display a summary at the bottom of each group, add a GroupFooter band to the report. For example, you can use the Group and Sort panel to do this.

    How to - CalculateCustomSum_0a

  3. Drop the UnitsOnOrder field from the Field List onto the created band.
  4. Click the label’s smart tag, and in its actions list, set the Summary Running property (XRSummary.Running) to Group to calculate the summary for each group in the report.

    Set the Summary Func (XRSummary.Func) to Custom and define a format string (XRControl.TextFormatString).

    How to - CalculateCustomSum

  5. When selecting the SummaryFunc.Custom option, three more events are added to the label’s events list: XRLabel.SummaryGetResult, XRLabel.SummaryReset and XRLabel.SummaryRowChanged.

    How to -  CalculateCustomSum1

    Subscribe to these events and handle them in the following way.

    // Declare a summary and a pack.
    double totalUnits = 0;
    double pack = 15;
    
    private void xrLabel1_SummaryReset(object sender, EventArgs e) {
        // Reset the result each time a group is printed.
        totalUnits = 0;
    }
    
    private void xrLabel1_SummaryRowChanged(object sender, EventArgs e) {
        // Calculate a summary.
        totalUnits += Convert.ToDouble(GetCurrentColumnValue("UnitsOnOrder"));
    }
    
    private void xrLabel1_SummaryGetResult(object sender, SummaryGetResultEventArgs e) {
        // Round the result, so that a pack will be taken into account
        // even if it contains only one unit.
        e.Result = Math.Ceiling(totalUnits / pack);
        e.Handled = true;
    }
    

You can now preview the report at runtime. Refer to the following topics to learn how to do this in different platforms:

Note

The runtime report customization that report events provide is not reflected in the design-time Preview. To perform the required changes, you can use scripts or expression bindings.