Create a Report Parameter
- 5 minutes to read
This topic demonstrates how to create a report parameter in the Visual Studio Report Designer and in code. The topic also describes the options you can specify for a report parameter.
Create a Report Parameter in the Report Designer
In the Visual Studio Report Designer, you can create a parameter from the Field List, Properties window, or FilterString Editor. The created parameter appears in the Field List‘s Parameters node.
Create From the Field List
Right-click the Parameters node in the Field List and select Add Parameter.
Specify parameter options in the invoked Add New Parameter dialog and click OK.
Create From the Properties window
Select a report and click the Parameters property’s ellipsis button in the Properties window.
Click Add in the invoked Report Parameters Editor dialog to add a new parameter. Specify parameter options and click OK.
Create From the FilterString Editor
You can create a parameter when you construct a condition in the FilterString Editor. Choose the Select a parameter field, click the field, and select Add Parameter in the invoked context menu.
Specify parameter options in the invoked Add New Parameter dialog and click OK.
Parameter Options
Name
The name by which you can reference a parameter in a report.
Note
Report parameters should have unique names. A name should not contain C# keywords or XtraReport‘s property names.
Description
A parameter description that appears on a report’s Print Preview in the Parameters Panel.
Type
The type of parameter values. The following types are available:
- String;
- Date;
- Number (16-bit integer);
- Number (32-bit integer);
- Number (64-bit integer);
- Number (floating point);
- Number (double-precision floating point);
- Number (decimal);
- Boolean;
- GUID (Globally Unique Identifier).
You can also create a parameter of a custom type.
Show in the Parameters Panel
Specifies whether a parameter is visible in the Parameters Panel.
Allow Null Value
When the Allow Null Value option is enabled, you can leave the parameter’s value unspecified.
Allow Multiple Values
When the Allow Multiple Values option is enabled, you can specify multiple values for a report parameter.
Select All Values
Enable the Select All Values option to use all elements from a custom set of values as a parameter’s default value.
Note
You can specify the Select All Values option only when the Allow Multiple Values option is enabled.
Default Value
A parameter’s default value. This value is displayed in the Parameters Panel when you open a report’s Print Preview.
Expression
The Expression option allows you to specify an expression for a parameter’s default value. For example, you can set this option to Now() to use the current date as a date parameter’s default value.
Note
You can use only constants, operators, and date-time / logical / math / string functions in a parameter’s Expression option.
Value Source
Use the Value Source option to specify a custom set of values a parameter can accept. You can create a static list of values, load values from a data source, or specify a date range. Refer to the following topics for more details:
- Report Parameters with Predefined Static Values
- Report Parameters with Predefined Dynamic Values
- Date Range Report Parameters
Create a Report Parameter in Code
To create a parameter in code, initialize a Parameter class instance and specify its properties. Add the parameter to the report’s Parameters collection.
The code sample below demonstrates how to create a date parameter, specify an expression for the parameter’s default value, and reference the parameter in a label‘s expression.
using System.Drawing;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Parameters;
using DevExpress.XtraReports.Expressions;
// ...
// Create a date report parameter.
// Use an expression to specify the parameter's default value.
var dateParameter = new Parameter() {
Name = "date",
Type = typeof(System.DateTime),
ExpressionBindings = { new BasicExpressionBinding("Value", "Now()") }
};
// Create a label and bind the label's Text property to the parameter value.
// Use the parameter's name to reference the parameter in the label's expression.
var dateLabel = new XRLabel() {
ExpressionBindings = { new ExpressionBinding("Text", "?date") },
BoundsF = new RectangleF(0, 0, 200, 50),
};
// Create a report and add the label to the report's Detail band.
var report = new XtraReport() {
Bands = { new DetailBand() { Controls = { dateLabel } } },
};
// Add the parameter to the report's Parameters collection.
report.Parameters.Add(dateParameter);