Skip to main content

CalculatedField Class

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v24.1.dll

NuGet Package: DevExpress.Reporting.Core

Declaration

public class CalculatedField :
    DataContainerComponent,
    IXtraSupportShouldSerialize,
    IScriptable,
    IDisplayNamePropertyContainer,
    ICalculatedField

Remarks

Calculated fields are accessed via the XtraReport.CalculatedFields property of a report. A value of a calculated field is obtained by evaluating its expression, represented by its CalculatedField.Expression property’s text. Both at design time within Visual Studio and in the End-User Designer, an expression of virtually any level of complexity can be easily constructed using the Expression Editor.

CalculatedField_ExpressionEditor

This expression can be built upon data fields (which are obtained from the data table defined by the CalculatedField.DataSource and CalculatedField.DataMember property values), report parameters, and different date-time, logical, math and string functions surrounding them. For a complete list of these functions, refer to Expressions.

A calculated field’s expression can evaluate values of other calculated fields if you avoid circular references.

To learn more, refer to Calculated Fields Overview. And, for an example on how to use calculated fields in a report, refer to the Using Calculated Fields tutorial.

Example

This example demonstrates how to create a calculated field at runtime, and bind the field to the report control’s Text property.

using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Configuration;
// ...
// Create a report.
XtraReport1 report = new XtraReport1();

// Create a calculated field 
// and add it to the report's collection.
CalculatedField calcField = new CalculatedField();
report.CalculatedFields.Add(calcField);

// Specify the calculated field's properties.
calcField.DataSource = report.DataSource;
calcField.DataMember = report.DataMember;
calcField.FieldType = FieldType.Double;
calcField.DisplayName = "Calculated Field";
calcField.Name = "myField";
calcField.Expression = "[UnitPrice] * [UnitsInStock]";

// Bind the label's Text property to the calculated field.
report.FindControl("xrlabel3", true).ExpressionBindings
            .Add(new ExpressionBinding() {
                EventName = "BeforePrint",
                PropertyName = "Text",
                Expression = "FormatString('{0:c2}', [myField])"
            });

Implements

Inheritance

Object
MarshalByRefObject
Component
DevExpress.XtraReports.UI.DataContainerComponent
CalculatedField
See Also