Example 4. Calculating total

Top  Previous  Next

In this example, we will show how to calculate the sum by using programming methods. We will use the following:

 

BeforePrint band event;
reference to the data column from a script;
local variable, whose value will be printed in the report.

 

Create a report of the following form:

 

scriptExample4

 

In the script, declare the "sum" variable and create a BeforePrint event handler belonging to the band:

 

public class ReportScript

{

  private decimal sum;

 

  private void Data1_BeforePrint(object sender, EventArgs e)

  {

    sum += (Decimal)Report.GetColumnValue("Products.UnitPrice");

  }

}

 

The "Products.UnitPrice" data column can be placed into the script, dragging it from the "Data" window.

 

If you run the report, you will see the following:

 

scriptExample4_1

 

The same effect can be achieved by using totals.