Handle Script Events to Calculate Custom Summaries

This example illustrates a legacy approach to performing custom calculations over a report's data by handling script events for report elements.

Important

Report scripts can never be considered entirely safe, and their execution cannot be completely restricted.

In particular, assigning security permissions to report scripts does not restrict them from accessing the .NET framework's static variables and methods.

Consider using binding expressions as the most convenient approach to performing custom calculations over your report's data.

In this example a report is bound to the "Products" table (from the Northwind Traders demo database shipped with XtraReports). The following image illustrates this report's design:

Scripting - CustomSummary1.png

The following are the scripts used to calculate the minimum value for the UnitPrice data field in the Products table of the Northwind database. Note that for this example to work correctly, the xrLabel1 should be bound to the UnitPrice field, its XRSummary.Running property should be set to Report, and its XRSummary.Func property should be set to Custom. Then, switch to the Scripts tab, and handle the following script events of this label.

System.Decimal minPrice = System.Decimal.MaxValue;

private void OnSummaryReset(object sender, System.EventArgs e) {
   minPrice = System.Decimal.MaxValue;
}

private void OnSummaryRowChanged(object sender, System.EventArgs e) {
    minPrice = Math.Min(minPrice, (System.Decimal)GetCurrentColumnValue("UnitPrice"));
}

private void OnSummaryGetResult(object sender, SummaryGetResultEventArgs e) {
    e.Result = minPrice;
    e.Handled = true;
}

The following are the scripts used to calculate the maximum value for the UnitPrice data field. Note that for this example to work correctly, the xrLabel2 should be bound to the UnitPrice field, its XRSummary.Running property should be set to Report, and its XRSummary.Func property should be set to Custom. Then, switch to the Scripts tab, and handle the following script events of this label.

System.Decimal maxPrice = System.Decimal.MinValue;

private void OnSummaryReset(object sender, System.EventArgs e) {
   maxPrice = System.Decimal.MinValue;
}

private void OnSummaryRowChanged(object sender, System.EventArgs e) {
    maxPrice = Math.Max(maxPrice, (System.Decimal)GetCurrentColumnValue("UnitPrice"));
}

private void OnSummaryGetResult(object sender, SummaryGetResultEventArgs e) {
    e.Result = maxPrice;
    e.Handled = true;
}

The following are the scripts used to calculate the average value for the UnitPrice data field. Note that for this example to work correctly, the xrLabel3 should be bound to the UnitPrice field, its XRSummary.Running property should be set to Report, and its XRSummary.Func property should be set to Custom. Then, switch to the Scripts tab, and handle the following script events of this label.

System.Decimal totalPrice = 0;
int productsCount = 0;

private void OnSummaryReset(object sender, System.EventArgs e) {
  totalPrice = 0;
  productsCount = 0;
}

private void OnSummaryRowChanged(object sender, System.EventArgs e) {
  productsCount++;
  totalPrice += (System.Decimal)GetCurrentColumnValue("UnitPrice");
}

private void OnSummaryGetResult(object sender, SummaryGetResultEventArgs e) {
    e.Result = productsCount > 0 ? totalPrice / productsCount : 0;
    e.Handled = true;
}

The following image illustrates the result:

Scripting - CustomSummary2.png

See Also