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

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

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

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

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

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

- Select the Label
- Click on the smart tag (">") button on the upper part of the selected Label
- 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

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

- Click on Report Explorer
- Select the C# field from the drop-down menu in the Scripts section
Create the Script

- Click on Script on the upper-right side of the window.
C# Script: Variable Declaration
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

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

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

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

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

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

Now we associate sub-routines to the events specified on the left column.
- Select the first Label
- On Property Grid, expand the Scripts section
- 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

- Select the second Label
- On Property Grid, expand the Scripts section
- 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

Select Preview in the Actions group in the Reports window
The Result
