How to Create Previous Page Totals with PixelPerfect C# Scripts

    Description: This tutorial illustrates how create a PixelPerfect Report with C# Scripts. This Report calculates previous page totals using macros. You can use C#, Visual Basic or JavaScript to develop PixelPerfect Scripts. For a Visual Basic version of this tutorial refer to "How to Create Previous Page Totals with PixelPerfect Visual Basic Scripts".

     

    Create a New PixelPerfect Report

     

    Create-a-New-PixelPerfect-Report.png

    1. Select Reports in the lower left-hand panel
    2. Select the PixelPerfect report type in the panel just above
    3. Select PixelPerfectReport in the New group of the tool bar.

    Name the Report

    Name-the-Report.png

    1. Enter "PixelPerfect Previous Page Total with C# Script" as name for your report
    2. Select New in the Template group of the tool bar.

    Create a Table

    Create-a-Table.png

    1. Add CH294 as a Level. For more explanation on the Level creation please refer to "How to Create a Simple PixelPerfect Report"
    2. Create the table into the report. Don't insert any column totals.

    Add a PageFooter

    Add-a-PageFooter.png

    1. Right-click on the empty area in the footer of the page
    2. Select Insert Band
    3. Choose PageFooter from the list.

    Fill-in the PageFooter

    Fill-in-the-PageFooter.png

    1. From the Tool Box window, select Label and "drag-and-drop" it
    2. Create six new Labels inside the PageFooter and rename the first three. Format these Labels as you prefer.

    Set Data Binding

    Set-Data-Binding.png

    1. Select the Label
    2. Click on the smart tag (">") button on the upper part of the selected Label
    3. Select the Total Sales field from the drop-down menu in the Data Binding section.
    Do these steps for the other two labels.

    Set Format String

    Set-Format-String.png

    1. Click on the smart tag (">")
    2. Click on the browse button at the far right end of the Format String field to open the FormatString Editor.
    3. Select Currency on the left side of the window
    4. Select $0.00 from the list
    5. Click OK.
    Do these steps for the other two labels.

    Select the Script Language

    Select-the-Script-Language.png

    1. Click on Report Explorer
    2. Select the C# field from the drop-down menu in the Scripts section

    Create the Script

    Create-the-Script.png

    1. Click on Script on the upper-right side of the window.

     

     

    C# Script: Variable Declaration

     

    C--Script--Variable-Declaration.png
    using System.Collections.Generic; double currentPageTotal = 0; double runningPagesTotal = 0; int PageNumber = 0; List<double>PageTotal=new List<double>();

    Add the System.Collections.Generic namespace to use Lists.

    • currentPageTotal: Total Sales on the current page
    • runningPagesTotal: Total Sales of all pages
    • PageNumber: Page counter
    • PageTotal: List with the total of each single page.
    Copy and paste this code inside the Script Editor.

     

    C# Script: OnSummaryReset Subroutine

    C--Script--OnSummaryReset-Subroutine.png

     

    private void OnSummaryReset_currentPageTotal(object sender, System.EventArgs e) { PageTotal.Add(currentPageTotal); currentPageTotal=0; }

    This sub-routine is executed at the beginning of each new page.

    Code:

    • Add the currentPageTotal value to the list
    • Save the currentPageTotal value inside the list and reset its value.
    Add this code below the previous code.

     

    C# Script: OnSummaryRowChanged

    C--Script--OnSummaryRowChanged.png

     

    private void OnSummaryRowChanged_currentPageTotal(object sender, System.EventArgs e) { currentPageTotal+=(double)DetailReport.GetCurrentColumnValue("Total Sales"); }

    This code is executed for each row on the page. It retrieves the value of the Total Sales field and adds it to the previous total. Remember to change the field name if you want to re-use the code in another field.

    Add this code below the previous code.

     

    C# Script: OnSummaryGetResult Current Page

    C--Script--OnSummaryGetResult-Current-Page.png

     

    private void OnSummaryGetResult_currentPageTotal(object sender, DevExpress.XtraReports.UI.SummaryGetResultEventArgs e) { e.Result=currentPageTotal; e.Handled=true; }

    This code returns the sum for the current page. It is executed when changing pages. Add this code below the previous code.

     

    C# Script: OnSummaryGetResult Previous Page

    C--Script--OnSummaryGetResult-Previous-Page.png

     

    private void OnSummaryGetResult_previousPageTotal(object sender, DevExpress.XtraReports.UI.SummaryGetResultEventArgs e) { e.Result=PageTotal[PageNumber]; e.Handled=true; PageNumber+=1; }

     

    This code returns the total for the previous page. Add this code below the previous code.

     

    C# Script: OnSummaryGetResult Total Sales

    C--Script--OnSummaryGetResult-Total-Sales.png

     

    private void OnSummaryGetResult_runningPagesTotal(object sender, DevExpress.XtraReports.UI.SummaryGetResultEventArgs e) { runningPagesTotal+=currentPageTotal; e.Result=runningPagesTotal; e.Handled=true; }

    This code increases the value of runningPagesTotal by the currentPageTotal and returns the value of the Total Sales for each preceding page. Add this code below the previous code.

     

    Associate the Sub-routines to the Events to Calculate Current Page Total

    Associate-the-Sub-routines-to-the-Events-to-Calcul.png

     

    Now we associate sub-routines to the events specified on the left column.

    1. Select the first Label
    2. On Property Grid, expand the Scripts section
    3. Insert:
    • OnSummaryGetResult_currentPageTotal on Summary Get Result field.
    • OnSummaryReset_currentPageTotal on Summary Reset field
    • OnSummaryRowChanged_currentPageTotal on Summary Row Change field.

    Associate the Sub-routines to the Events to Calculate Preceding Page Total

    Associate-the-Sub-routines-to-the-Events-to-Calcul-1.png

    1. Select the second Label
    2. On Property Grid, expand the Scripts section
    3. Insert:
    • OnSummaryGetResult_previousPageTotal on Summary Get Result field.

    Do these steps also for the last Label. Insert OnSummaryGetResult_runningPagesTotal on Summary Get Result field.

     

     

    Generate Preview

    Generate-Preview.png

     

    Select Preview in the Actions group in the Reports window

     

    The Result

    The-Result.png