Skip to main content

Handle Script Events of Report Elements

  • 4 minutes to read

This document describes how to handle report elements’ script events.

Important

Report scripts are not secure. We recommend that you use expression bindings instead.

Running the following code requires that your application contains a report bound to the “Products” table of the sample Northwind database.

// === Detail.Scripts.OnBeforePrint ===
private void OnBeforePrint(object sender, System.ComponentModel.EventArgs 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.ComponentModel.EventArgs 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