Back to Devexpress

Handle Script Events of Report Elements

xtrareports-10657-feature-guide-to-devexpress-reports-reporting-api-use-report-scripts-handle-script-events-of-report-elements.md

latest8.0 KB
Original Source

Handle Script Events of Report Elements

  • Feb 18, 2026
  • 4 minutes to read

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

Important

Report scripts are not secure and are disabled by default. 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.

csharp
// === 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 ===
vb
' === Detail.Scripts.OnBeforePrint ===
Private Sub OnBeforePrint(sender As Object, e As System.ComponentModel.EventArgs)
   Dim cells() As XRTableCell = {pidCell, productNameCell, productPriceCell}
   Dim price As System.Decimal = GetCurrentColumnValue("UnitPrice")
   If price < 20 Then
      ChangeCellsColor(cells, Color.Red)
   Else
      If price > 60 Then
         ChangeCellsColor(cells, Color.Green)
      Else
         ChangeCellsColor(cells, Color.Black)
      End If
   End If 
End Sub

Sub ChangeCellsColor(cells() As XRTableCell, color As Color)
   Dim count As Integer = cells.Length
   Dim i As Integer
   For i = 0 To count - 1
      cells(i).ForeColor = color
   Next i
End Sub
' === Detail.Scripts.OnBeforePrint ===

' === xrLabel1.Scripts.OnSummaryReset ===
Imports MyAssembly

Dim minPrice As System.Decimal = System.Decimal.MaxValue

Private Sub OnSummaryReset(ByVal sender As Object, ByVal e As System.EventArgs)
   minPrice = System.Decimal.MaxValue
End Sub
' === xrLabel1.Scripts.OnSummaryReset ===

' === xrLabel1.Scripts.OnSummaryRowChanged ===
Private Sub OnSummaryRowChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    minPrice = Math.Min(minPrice, GetCurrentColumnValue("UnitPrice"))
End Sub
' === xrLabel1.Scripts.OnSummaryRowChanged ===

' === xrLabel1.Scripts.OnSummaryGetResult ===
Private Sub OnSummaryGetResult(ByVal sender As Object, ByVal e As SummaryGetResultEventArgs)
    e.Result = minPrice
    e.Handled = True
End Sub
' === xrLabel1.Scripts.OnSummaryGetResult ===

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

csharp
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;
      }
   }
}
vb
Namespace AutogeneratedNamespace

   Imports System
   Imports System.Collections
   Imports System.Drawing
   Imports DevExpress.Data
   Imports DevExpress.Utils
   Imports DevExpress.XtraPrinting
   Imports DevExpress.XtraReports
   Imports DevExpress.XtraReports.UI

   Imports MyAssembly
   ' other usings

   Class AutogeneratedClass
      Private pidCell As XRTableCell
      Private productNameCell As XRTableCell
      Private productPriceCell As XRTableCell
      Private XtraReport1 As XtraReport

      ' other variables
      Private Sub DetailOnBeforePrint(sender As Object, _ 
      e As System.ComponentModel.EventArgs)
         Dim cells() As XRTableCell = {pidCell, productNameCell, productPriceCell}
         Dim price As System.Decimal = GetCurrentColumnValue("UnitPrice")
         If price < 20 Then
            ChangeCellsColor(cells, Color.Red)
         Else
            If price > 60 Then
               ChangeCellsColor(cells, Color.Green)
            Else
               ChangeCellsColor(cells, Color.Black)
            End If
         End If 
      End Sub

      Sub ChangeCellsColor(cells() As XRTableCell, color As Color)
         Dim count As Integer = cells.Length
         Dim i As Integer
         For i = 0 To count - 1
            cells(i).ForeColor = color
         Next i
      End Sub

      Private minPrice As System.Decimal = System.Decimal.MaxValue

      Private Sub xrLabel1OnSummaryReset(ByVal sender As Object, ByVal e As System.EventArgs)
         minPrice = System.Decimal.MaxValue
      End Sub

      Private Sub xrLabel1OnSummaryRowChanged(ByVal sender As Object, _ 
      ByVal e As System.EventArgs)
         minPrice = Math.Min(minPrice, GetCurrentColumnValue("UnitPrice"))
      End Sub

      Private Sub xrLabel1OnSummaryGetResult(ByVal sender As Object, _ 
      ByVal e As SummaryGetResultEventArgs)
         e.Result = minPrice
         e.Handled = True
      End Sub
   End Class
End Namespace

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:

See Also

Scripts - Security Considerations

DevExpress Reporting - Security Considerations