Example E1489
Visible to All Users

How to Create a Calculated Field at Runtime

This example creates a calculated field at runtime and binds it to the label's Text property.

Report with calculated field

Files to Review:

Documentation:

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

UsingCalculatedFields/Form1.cs(vb)
C#
using System; using System.Windows.Forms; using DevExpress.XtraReports.UI; using DevExpress.XtraReports.Configuration; namespace UsingCalculatedFields { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 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])" }); // Preview the report. ReportPrintTool printTool = new ReportPrintTool(report); printTool.ShowPreviewDialog(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.