本示例演示了在运行时刻如何创建 计算字段,并绑定到控件的 XRControl.Text 属性。

C#CopyCode image复制代码
using System;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
// ...

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);

    // Define its properties.
    calcField.DataSource = report.DataSource;
    calcField.DataMember = report.DataMember;
    calcField.FieldType = FieldType.Double;
    calcField.DisplayName = "A Calculated Field";
    calcField.Name = "myField";
    calcField.Expression = "[UnitPrice] * [Quantity]";

    // Bind a label's Text property to the calculated field.
    report.FindControl("xrlabel2", true).DataBindings.Add("Text", null, "Order Details.myField");

    // Display the report.
    report.ShowPreview();
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UI
' ...

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click

    ' Create a report.
    Dim report As New XtraReport1()

    ' Create a calculated field, 
    ' and add it to the report's collection.
    Dim calcField As New CalculatedField()
    report.CalculatedFields.Add(calcField)

    ' Define its properties.
    calcField.DataSource = report.DataSource
    calcField.DataMember = report.DataMember
    calcField.FieldType = FieldType.Double
    calcField.DisplayName = "A Calculated Field"
    calcField.Name = "myField"
    calcField.Expression = "[UnitPrice] * [Quantity]"

    ' Bind a label's Text property to the calculated field.
    report.FindControl("xrlabel2", True).DataBindings.Add("Text", Nothing, "Order Details.myField")

    ' Display the report.
    report.ShowPreview()
End Sub

在下面的插图中显示了结果。

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E1489。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

Expand image参阅