Handle Script Events of Report Elements

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.

Running the following code requires that your application contains a report bound to the "Products" table of a demo Northwind database (the nwind.mdb file located in the directory where you installed DevExpress demos).

// === Detail.Scripts.OnBeforePrint ===
private void OnBeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
   XRTableCell[] cells = new XRTableCell[] { pidCell, productNameCell, productPriceCell };
   System.Decimal price = (System.Decimal)GetCurrentColumnValue("UnitPrice");
   if (price < 20)
      ChangeCellsColor(cells, Color.Red);
   else if (price > 60)
      ChangeCellsColor(cells, Color.Green);
   else
      ChangeCellsColor(cells, Color.Black);
}

void ChangeCellsColor(XRTableCell[] cells, Color color) {
   int count = cells.Length;
   for (int i = 0; i < count; i++)
      cells[i].ForeColor = color;
}
// === Detail.Scripts.OnBeforePrint ===


// === xrLabel1.Scripts.OnSummaryReset ===
using MyAssembly;

System.Decimal minPrice = System.Decimal.MaxValue;

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

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

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

The following internal code is automatically generated by XtraReports after running the above scripts. This code is for internal use only.

namespace AutogeneratedNamespace
{
   using System;
   using System.Collections;
   using System.Drawing;
   using DevExpress.Data;
   using DevExpress.Utils;
   using DevExpress.XtraPrinting;
   using DevExpress.XtraReports;
   using DevExpress.XtraReports.UI;

   using MyAssembly;
   // other usings

   class AutogeneratedClass
   {
      private XRTableCell pidCell;
      private XRTableCell productNameCell;
      private XRTableCell productPriceCell;
      private XtraReport XtraReport1;
      // other variables

      private void DetailOnBeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
         XRTableCell[] cells = new XRTableCell[] { pidCell, productNameCell, productPriceCell };
         System.Decimal price = (System.Decimal)GetCurrentColumnValue("UnitPrice");
         if (price < 20)
            ChangeCellsColor(cells, Color.Red);
         else if (price > 60)
            ChangeCellsColor(cells, Color.Green);
         else
            ChangeCellsColor(cells, Color.Black);
      }

      void ChangeCellsColor(XRTableCell[] cells, Color color) {
         int count = cells.Length;
         for (int i = 0; i < count; i++)
            cells[i].ForeColor = color;
      }

      System.Decimal minPrice = System.Decimal.MaxValue;

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

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

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

The scripts' execution occurs on loading a report's Print Preview (either at runtime or at design time in Visual Studio).

The following image shows the results:

Scripting - ScriptingBasics2

See Also