Show Me |
---|
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E2385。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。 |
下面的示例演示了在运行时刻如何把脚本添加到报表控件。
要完成此任务,首先需要把所需的代码添加到所有脚本的集合中,所有脚本都被存储在 XtraReport.ScriptsSource 属性。 然后,把脚本的名称指派到控件的 XRControlEvents.OnBeforePrint 属性,这样,当适当的事件被报表生成引擎触发时,将会导致脚本代码被执行。
C# | 复制代码 |
---|---|
(Form1.cs) using System; using System.Windows.Forms; using DevExpress.XtraReports.UI; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { new ReportPrintTool(CreateReport()).ShowPreviewDialog(); } private void button2_Click(object sender, EventArgs e) { new ReportDesignTool(CreateReport()).ShowDesignerDialog(); } private XtraReport CreateReport() { // Create a new report. XtraReport1 report = new XtraReport1(); // Add a script to a report's collection of scripts. report.ScriptsSource += "private void xrLabel1_BeforePrint(object sender, " + "System.Drawing.Printing.PrintEventArgs e) {\r\n " + "((XRLabel)sender).BackColor = Color.Aqua;\r\n}"; // Assign this script to a label's BeforePrint script. report.xrLabel1.Scripts.OnBeforePrint = "xrLabel1_BeforePrint"; return report; } } } |
Visual Basic | 复制代码 |
---|---|
(Form1.vb) Imports Microsoft.VisualBasic Imports System Imports System.Windows.Forms Imports DevExpress.XtraReports.UI Namespace WindowsApplication1 Partial Public Class Form1 Inherits Form Public Sub New() InitializeComponent() End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles button1.Click CType(New ReportPrintTool(CreateReport()), ReportPrintTool).ShowPreviewDialog() End Sub Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles button2.Click CType(New ReportDesignTool(CreateReport()), ReportDesignTool).ShowDesignerDialog() End Sub Private Function CreateReport() As XtraReport ' Create a new report. Dim report As New XtraReport1() ' Add a script to a report's collection of scripts. report.ScriptsSource &= "private void xrLabel1_BeforePrint(object sender, " & _ "System.Drawing.Printing.PrintEventArgs e) {" & Constants.vbCrLf & " " & _ "((XRLabel)sender).BackColor = Color.Aqua;" & Constants.vbCrLf & "}" ' Assign this script to a label's BeforePrint script. report.xrLabel1.Scripts.OnBeforePrint = "xrLabel1_BeforePrint" Return report End Function End Class End Namespace |