Calculated Fields

  • 2 minutes to read

This document describes how to use calculated fields to evaluate custom expressions based on external data, and embed the results into a Snap document.

The document consists of the following sections.

Add Calculated Fields

Use calculated fields to perform additional calculations on dynamic data and embed the results into a published document.

To create a calculated field and access its collection, invoke the context menu in the Data Explorer.

Snap_AddCalculatedFields

A calculated field with the default name (calculatedField1, calculatedField2 etc.) is created. Use Edit Calculated Fields... context menu to change the name and type of the field.

NOTE

The field's name should begin with a letter or an underscore and contain only alphanumeric characters.

A calculated field's expression can include conditional, date-time, mathematical and other formulas. It can also evaluate the values of other calculated fields, data source fields and parameters.

Snap-ExpressionEditor

The Expression Editor is invoked by right-clicking a calculated field and choosing the Edit Expression… item in the invoked popup menu.

Calculated-Fields-01

After a calculated field is inserted into a document, its value is evaluated each time the document is set to be published.

Create Calculated Fields in Code

Use the DataSourceInfo.CalculatedFields property to access the collection of CalculatedField objects in the data source. The IDataSourceOwner.CalculatedFields property provides access to the snap document's collection of calculated fields.

A calculated field's value is returned after evaluating an expression assigned to the CalculatedField.Expression property.

The following code illustrates how to create and customize a calculated field.


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using DevExpress.Snap.Core.API;
// ...

private void Form1_Load(object sender, EventArgs e) {
    CalculatedField calcField = new CalculatedField("Amount", "");
    calcField.FieldType = DevExpress.XtraReports.UI.FieldType.Float;
    calcField.Expression = "[UnitPrice]*[UnitsInStock]";
    snapControl1.Document.CalculatedFields.Add(calcField);
}
See Also