这个示例展示了在运行时刻如何创建一个有 参数 的报表。

在参数被添加到报表之后,其取值就可以在报表的 XtraReportBase.FilterString 中使用,或者在 XRLabel 控件中显示。

然后当预览报表时,最终用户可以通过 参数 用户界面(使用 ReportPrintTool.AutoShowParametersPanel 属性启用) 来修改参数值。

C#CopyCode image复制代码
using System;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Parameters;
// ...

private void simpleButton1_Click(object sender, EventArgs e) {
    // Create a report instance.
    XtraReport1 report = new XtraReport1();

    // Create a parameter and specify its name.
    Parameter param1 = new Parameter();
    param1.Name = "CatID";

    // Specify other parameter properties.
    param1.Type = typeof(System.Int32);
    param1.Value = 1;
    param1.Description = "Category: ";
    param1.Visible = true;

    // Add the parameter to the report.
    report.Parameters.Add(param1);

    // Specify the report's filter string.
    report.FilterString = "[CategoryID] = [Parameters.CatID]";

    // Force the report creation without previously 
    // requesting the parameter value from end-users.
    report.RequestParameters = false;

    // Show the parameter's value on a Report Header band.
    XRLabel label = new XRLabel();
    label.DataBindings.Add(new XRBinding(param1, "Text", "Category: {0}"));
    ReportHeaderBand reportHeader = new ReportHeaderBand();
    reportHeader.Controls.Add(label);
    report.Bands.Add(reportHeader);

    // Assign the report to a ReportPrintTool,
    // to hide the Parameters panel,
    // and show the report's print preview.
    ReportPrintTool pt = new ReportPrintTool(report);
    pt.AutoShowParametersPanel = true;
    pt.ShowPreviewDialog();
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraReports.Parameters
' ...

Private Sub simpleButton1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles simpleButton1.Click
    ' Create a report instance.
    Dim report As New XtraReport1()

    ' Create a parameter and specify its name.
    Dim param1 As New Parameter()
    param1.Name = "CatID"

    ' Specify other parameter properties.
    param1.Type = GetType(System.Int32)
    param1.Value = 1
    param1.Description = "Category: "
    param1.Visible = True

    ' Add the parameter to the report.
    report.Parameters.Add(param1)

    ' Specify the report's filter string.
    report.FilterString = "[CategoryID] = [Parameters.CatID]"

    ' Force the report creation without previously 
    ' requesting the parameter value from end-users.
    report.RequestParameters = False

    ' Show the parameter's value on a Report Header band.
    Dim label As New XRLabel()
    label.DataBindings.Add(New XRBinding(param1, "Text", "Category: {0}"))
    Dim reportHeader As New ReportHeaderBand()
    reportHeader.Controls.Add(label)
    report.Bands.Add(reportHeader)

    ' Assign the report to a ReportPrintTool,
    ' to hide the Parameters panel,
    ' and show the report's print preview.
    Dim pt As New ReportPrintTool(report)
    pt.AutoShowParametersPanel = True
    pt.ShowPreviewDialog()
End Sub

在下面的插图中显示了结果。

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E546。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

Expand image参阅