Skip to main content

How to: Calculate Page and Report Summaries

  • 3 minutes to read

This topic illustrates the use of PrintingSystem events to adjust the displayed information when page contents are changed due to margin or page size modifications.

The task is to implement summary fields that only calculate those values in the table which fall on one page. So, when the page size or margins are modified and the table with values becomes split across several pages, these summaries count values on each page. The page footer is a good place to put those fields.

The Summaries demo application in a demo suite for XtraPrinting Library provides an example of how this problem can be solved. Here, we point out methods used in this solution.

The report created in this application contains two columns with pseudo-random values. They are generated in the Detail section of the report. Each TextBrick containing a random value is marked as belonging to either the “Count” or “Sum” column. It is accomplished by assigning the ID property value. The ReportFooter section at the bottom displays two different summary types - item count and item sum, which are calculated when text bricks are generated and added to the Details area.

To represent the dynamic summaries, we create two text bricks in the BrickModifier.MarginalFooter page area and place them into an array for easy access when necessary. We choose the PrintingSystemBase.BeforePagePaint event of the PrintingSystem to perform a new summary calculation. It forces the recalculation each time a page is redrawn. The code of the custom PagePainting procedure, which handles this event, is presented below. It iterates the bricks on a current page, filters them by their identifier, performs the necessary calculations and puts the results into text bricks previously created in the MarginalFooter. To get access to the current page’s bricks, the CompositeBrick.GetEnumerator method can be used, and in code it is represented with the foreach statement.

private void PagePainting(object sender, PageEventArgs e) {
    double sum = 0, v;
    int count = 0;

    // Perform actions for all bricks on the current page.
    foreach (TextBrick brick in e.Page) {
        if (brick != null) {
            double d;
            if (!(brick.TextValue is string) || !double.TryParse((string)brick.TextValue, 
                out d)) continue;
            v = Convert.ToDouble(brick.TextValue);

            // Check the brick identifier and perform calculations accordingly. 
            switch (brick.ID) {
                case "Count":
                    count++;
                    break;
                case "Sum":
                    sum += v;
                    break;
            }
        }
    }

    // Assign the resulting values to the bricks, which represent 
    // totals in the page footer.
    ((TextBrick)sbriks[0]).Text = String.Format("{0:0.##}", count); 
    ((TextBrick)sbriks[1]).Text = String.Format("{0:0.##}", sum); 
}

The resulting report is shown in the following pictures. The first picture displays the report fitted onto one page, the second illustrates the situation when page size and margins are reduced, so the report is split into two pages.

DynamicFooterSummaries-01

DynamicFooterSummaries-02

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e18/reporting-for-winforms-how-to-calculate-page-and-report-summaries.

See Also