HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Sunday, July 22, 2012

How to: Create a Report with Parameters (Runtime Sample)

This example demonstrates how to create a report with a parameter at runtime.

After a parameter is added to the report, its value can be used in the report's XtraReportBase.FilterString, or displayed in an XRLabel control.

Then, when the report is being previewed, an end-user can modify the parameter's value via the Parameters UI, which is enabled using the ReportPrintTool.AutoShowParametersPanel property.

C#

VB

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();
}

The result is shown in the following image.

Show Me

The complete sample project is available in the DevExpress Code Central database at http://www.devexpress.com/example=E546. Depending on the target platform type (ASP.NET, WinForms, etc), you can either run this example online or download an auto-executable sample.

 

No comments:

Post a Comment