The main purpose of expressions is to bind report controls to data.
Many expressions consist only of a field name, displayed in brackets.
However, expressions can also be more complex
since they support a powerful language syntax including operators and function calls.
This enables you to perform calculations, evaluate conditions, and even trigger your own business logic functions.
You can use expressions to calculate or retrieve values not only for primary control properties like Text
but also for many appearance and layout properties.
It is possible to extend the list of supported properties, if your application requires it.
I begin with a report that includes master and detail levels for a demo dataset of cars and trips.
This provides a playing field for an expression introduction.
To start, please be aware that expressions are not always needed to show data in report elements.
I bring up the Smart Tag menu for the car table cell, and you can see that the Expression reflects the field name.
In the Designer Options dialog, you can see that data bindings use expressions by default.
Expressions are more powerful, but for some simple cases the older binding system is also still useful.
To demonstrate this, I first remove the entry in the Expression field.
Then I enter [car] into the Text field.
This is the same syntax that was previously used for the expression
and a quick switch to the Preview page shows that the report output remains the same with this change.
Back in the designer, I double-click the cell to edit its contents directly.
I enter a new string, which includes two fields.
I remove the VIN column from the table
and switch to the Preview again.
You can see how the two fields have been merged with the text elements for a combined output.
This mechanism does not use expressions, but it is very easy to use for basic text merging.
I will change the configuration now to take advantage of expressions instead.
I reset the Text property on the table cell.
Then I bring up the Expression Editor from the Smart Tag menu.
This dialog has an editing area at the top, and three columns at the bottom.
The left column provides lists of all the built-in elements
like functions, variables, and operators, and of reference items like field names.
The center column displays a sub-selection when I activate one of the items in the left column
and the right column shows help information
that can be useful to discover the syntax required for function calls, or the meaning of specific variables.
The system understands different kinds of expressions.
I begin by entering a concatenation to represent the same output I had before.
A quick look at the Preview confirms that this works correctly.
However, this is an opportunity to take advantage of a helper function
instead of putting the string together from five parts, the FormatString function generates it in one step
just like you might do it in C#.
I remove the expression in the Editor.
I find the FormatString function in the Functions/String category.
You can see the description on the right-hand side
including an example that indicates how value formats can be applied to individual elements.
The documentation has all the details
but basically this format function provides functionality very similar to the .NET String.Format function.
I enter a call to the function in the editor panel, to once more generate the same string as before.
It's easy to understand the expression syntax, only a few points require special attention.
Strings are delimited with single quotes, and the convention is to use brackets to delimit field names.
Unless your field names contain spaces, you can leave out the brackets, but the editor will add them automatically when you leave it.
A quick look at the preview confirms that the function call works correctly.
For a more complicated example, I expand the height of the main level detail band and add an XRLabel from the toolbox.
I resize the label to provide enough space, and then I configure the expression to execute a complicated calculation.
The expression accesses the trips collection associated with a Car row on the main level.
The format string uses a value placeholder that formats with two decimal places.
An extra expression in brackets is used to filter the collection, returning those trips that have a length over 200 miles.
Finally, the function avg is executed to calculate an average value.
The field specifier length is used to extract the value from each trip object.
The overall result is the average length of all trips longer than 200 miles.
This example illustrates how expressions can be used for calculations of almost any complexity.
I activate the preview again for the report, and you can see that the value is calculated once for each row on the master level.
Now it is time to clarify one point about expressions that can be confusing initially.
Looking at the Smart Tag menu of the last label I added, you see the Text property and the expression assigned at the same time.
Clearly the expression takes precedence, but you may wonder why the Text property has two different value sources at once.
By displaying the two entries side by side
the Smart Tag menu hides the technical fact that the expression is used to calculate values for the Text property.
Expressions can also be used for other properties
and I switch to a special page now in the Visual Studio Properties window to illustrate this.
The Expressions page includes all properties that are configured by default to accept expressions.
Note that it is possible to extend the mechanism to additional properties, if this is convenient for your purposes.
At this time, you can see the expression I created for the Text property.
You can also assign expressions to calculate values for visual appearance properties, layout properties, and even control visibility.
As an example, I select the length table cell in the detail report and bring up the Expression dialog for the BackColor property.
I assign an expression that returns different colors depending on the value in the length field.
Now the preview shows the length values color-coded on the basis of this expression.
This concludes the introduction of report expressions.
You have seen that they cover use cases from simple field bindings all the way to dynamic assignments of appearance parameters.
If you're interested in our Reports, you can watch other reporting videos from our playlist
Don't forget to subscribe to our channel and ring the bell so you get notified anytime we release new content.
Thanks for watching, and thank you for choosing DevExpress.