Specify Parameter Values
- 4 minutes to read
Specify Parameter Values in the Report Designer
Use the Parameters Panel to specify parameter values in the Visual Studio Report Designer:
Specify Parameter Values in Code
To specify a value for a report parameter in code, reference the parameter by name in the report’s Parameters collection and assign the value to the parameter’s Value property.
using System;
// ...
// Create a report instance.
XtraReport1 report = new XtraReport1();
// Reference a parameter by name in the report's Parameters
// collection and assign a value to the parameter's Value property.
report.Parameters["parameter1"].Value = 30;
Specify the parameter’s value before you generate the report’s document (call the CreateDocument() method) or invoke the report’s Print Preview.
You can also update the parameter’s value in the report’s BeforePrint event handler:
private void XtraReport1_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
(sender as XtraReport1).Parameters["parameter1"].Value = 42;
}
What if the Parameter Value Is an Expression
If a report parameter’s Value property is set to an expression, assigning a static value to this property in code has no effect. When you open the report Preview, the expression value is displayed instead of the assigned static value.
If you want to change the default expression value to a static value in code, remove the expression from the parameter’s ExpressionBindings collection and then assign the static value to the parameter’s Value property:
using DevExpress.XtraReports.UI;
using System;
using System.Linq;
// ...
var report = new XtraReport();
report.LoadLayoutFromXml("../Path/To/Report.repx");
var paramName = "date";
var parameter = report.Parameters[paramName];
var paramValueExpression = parameter.ExpressionBindings
.FirstOrDefault(p => p.PropertyName == "Value");
// Remove an expression from the report's ExpressionBindings collection.
if (paramValueExpression != null) {
parameter.ExpressionBindings.Remove(paramValueExpression);
}
// Assign a static value to the parameter's Value property.
report.Parameters[paramName].Value = DateTime.Parse("02/05/2022");
What if the Parameter is a Date Range
Date range parameters have a special structure. An expression for the parameter value is not assigned to the range parameter itself. Instead, it is assigned to its child parameters accessible via the RangeParametersSettings class properties:
using DevExpress.XtraReports.Parameters;
using DevExpress.XtraReports.UI;
using System;
using System.Linq;
// ...
static void AssignRangeParameters(XtraReport report, DateTime startDate, DateTime endDate) {
var paramName = "dateRange";
var parameter = report.Parameters[paramName];
if (parameter.ValueSourceSettings is RangeParametersSettings rangeSettings) {
ClearValueExpression(rangeSettings.StartParameter);
ClearValueExpression(rangeSettings.EndParameter);
}
parameter.Value = new Range<DateTime>(startDate, endDate);
}
static void ClearValueExpression(Parameter parameter) {
var paramValueExpression = parameter.ExpressionBindings
.FirstOrDefault(p => p.PropertyName == "Value");
// Remove an expression from the report's ExpressionBindings collection.
if (paramValueExpression != null) {
parameter.ExpressionBindings.Remove(paramValueExpression);
}
}
Specify Parameter Values in a Web Reporting Application
Refer to the following topics for details:
- Specify Parameter Values in a Web Forms Reporting Application
- Specify Parameter Values in an ASP.NET MVC Reporting Application
- Specify Parameter Values in an ASP.NET Core Reporting Application
- Specify Parameter Values in a Blazor Reporting Application
- Specify Parameter Values in a Blazor Report Viewer (Native) Application
- Specify Parameter Values in an Angular Reporting Application