Bind Report Controls to Data (Expression Bindings)
- 3 minutes to read
This topic describes how to bind report controls to data fields in the expression bindings mode.
Bind Report Controls at Design Time
After connecting a report to data, the Field List panel displays the data source’s hierarchy and provides access to the available data fields. This panel allows you to bind report controls to data in all binding modes.
Dropping a data field onto a report’s surface creates a new report control bound to the corresponding field.
Dropping a data field onto an existing control binds this control to the corresponding field.
See the Field List document for more information about using this panel for data binding.
Note that while binding, the field is assigned to the control’s property with the DefaultBindableProperty attribute (usually, XRControl.Text).
You can also bind a control to the calculated fields and report parameters, and combine static and dynamic content in the same control (for instance, to append a text prefix or postfix to a value obtained from a database).
When the UserDesignerOptions.DataBindingMode is set to DataBindingMode.Expressions or DataBindingMode.ExpressionsAdvanced, a report uses expressions to provide data to controls. In these modes, the Properties grid contains the Expressions tab. This tab provides properties that you can specify using expressions with available data fields.
The DataBindingMode.Expressions (simplified) is the default binding mode, in which you can provide data to report controls in the XRControl.BeforePrint event.
The DataBindingMode.ExpressionsAdvanced mode allows you to provide data to report controls in the XRControl.BeforePrint and XRControl.PrintOnPage events. You can use the events’ arguments that are available in the Expression Editor’s Variables section.
You can also access expressions for the most popular properties using the control’s smart tag. Clicking the property’s ellipsis button invokes the Expression Editor in which you can specify the required data field or construct a complex binding expression involving two or more data fields.
You do not need to run the Expression Editor if you want to bind a report control to a single data field. Click the control’s smart tag, invoke the Expression‘s drop-down list and select the required field.
After you bind the report control to data, you can format the control’s values using the Format String option (the XRControl.TextFormatString property).
You can also use a binding expression to shape report data (for instance, calculate summary results or conditionally format data). See the Shape Data (Expression Bindings) documentation section for more information.
Bind Report Controls at Runtime
The XRControl.ExpressionBindings property provides access to the control’s expression bindings. An ExpressionBinding object implements each expression binding’s functionality and contains the following settings:
- ExpressionBinding.EventName - specifies the event handler that evaluates the expression.
- ExpressionBinding.PropertyName - defines the property to which the expression is applied.
- ExpressionBinding.Expression - specifies the expression.
You can specify a report control’s binding expression at runtime by creating an ExpressionBinding object with the required settings and adding it to the XRControl.ExpressionBindings collection. The following code snippet demonstrates how to specify a custom expression for a label’s XRControl.Text property.
using DevExpress.XtraReports.UI;
public XtraReport1() {
// ...
ExpressionBinding expressionBinding = new ExpressionBinding("BeforePrint", "Text", "[UnitPrice]*[UnitsInStock]");
xrLabel1.ExpressionBindings.Add(expressionBinding);
}