Report parameters have names, description and value. The value of a parameter can be any object, including expression.
Report parameters can be added to a report through the Parameters collection of the Report class. Parameters can be added either programmatically or through the built-in editor inside a designer.
To add a parameter in code, create an instance of this class, specify its name, value, and optionally description and type (through the Name, Value, Description, and Type properties respectively), and add it to the Parameters collection. The type of a parameter is determined automatically from the value. However, setting the type explicitly is required when the parameter value is an expression. Otherwise, the value will be interpreted as string. The following code illustrates how to create a parameter with expression value in code:
C# Copy Code |
---|
ReportParameter parameter = new ReportParameter("DateCreated", "[Now()]"); |
Visual Basic Copy Code |
---|
Dim parameter As New ReportParameter("DateCreated", "[Now()]") |
To add a report parameter in the designer, select the report, navigate to the Parameters property in the property grid and press the 'Browse' button. This will display the ReportParameter Collection Editor as illustrated by the following image:
Click the 'Add' Button at the button to create a new parameter, select the parameter in the list, then fill in its properties in the property grid at the right side. The value editor in the property grid changes dynamically based on the selected type.
Report parameters can be referenced in expressions by name. To access a report parameter, prefix its name with "Properties." or "Report.". The following example demonstrates an expression that references the DateCreated parameter defined earlier:
Expression Copy Code |
---|
The report was created on [Parameters.DateCreated.Value] |
The Value member reference above is optional. Specifying [Parameters.DateCreated] will yield the same result. If the value of a parameter is an expression, it will be evaluated when the parameter is accessed.
The name, type or description of the parameter can be referenced using the same syntax. For example, the following expression displays the description of a parameter:
Expression Copy Code |
---|
[Parameters.MyParam.Description] |
If the value of the report parameter is a custom object, the properties of this object can be accessed in the same manner.
It is possible to reference report parameters within the value of other parameters. This needs to be done with caution to avoid cyclic referencing, which will result in an exception.