Using Expressions in a ReportViewer Report
You can include Microsoft Visual Basic expressions in ReportViewer reports. You can use expressions to calculate the value of a report item, or to calculate values for style and formatting properties or other report item properties. You can use all of the Visual Basic functions as well as built-in functions that are supported only in report definition files.
For more information about expressions that are commonly used in ReportViewer reports, see Common Expressions for ReportViewer Reports.
The most basic type of expression is one that displays a field value in a text box. This is called a field expression. To link a data field to a report item, the expression must include the Fields collection, the name of the field, and the Value property. The value is created for you automatically when you drag the field to the report. The following example shows an expression that displays a product name in a text box:
An expression can be a short expression that refers to a field object or a long expression that supports decision functions or formatting based on fields or other report items. Expressions in report items and properties must begin with an equal character (=). If you do not begin text with this character, the text will be evaluated as the actual value of the field.
Examples of field expressions are as follows:
This expression concatenates the FirstName field and the LastName field.
=Fields!FirstName.Value & " " & Fields!LastName.Value
This expression performs a sum aggregation on the LineTotal field
For more information about the Fields collection, see Global Collections for Expressions in ReportViewer Reports.
You can use expressions to control the appearance of a report item. For example, you can write an expression for the Color property of a text box that displays the data in a different color depending on the data. An example of conditional formatting is as follows.
This expression, used in the Color property of a text box, displays the value in red if the value in the Cost field is greater than the value in the Revenue field. If the condition is not met, the text appears black.
=IIf(Fields!Cost.Value > Fields!Revenue.Value, "Red", "Black")
For more information about conditional formatting, see Adding Style and Formatting to a ReportViewer Report.
Reporting Services Functions
Reporting Services provides a host of built-in functions that you can use in expressions. These include standard aggregate functions like Sum, Min, Max, and Count, in addition to functions like RowNumber and RunningValue. For a full list of functions with descriptions, see Built-in Functions for ReportViewer Reports.
Class References and Custom Code
Expressions within a report can also contain references to the classes within the Microsoft.VisualBasic, System.Convert, and System.Math namespaces. If you use classes or functions from other system namespaces, you must use the full namespace, for example, System.Collections.ArrayList.
If you need additional functionality not provided by the Reporting Services functions or by the default classes, you can use custom code, another standard assembly, or a report code block. You do this by building a custom assembly and then by referencing that assembly from within an expression. For more information, see Adding Custom Code to a ReportViewer Report.