本示例代码说明了如何把报表绑定到存储在文件 (本例为 Cars.xml) 中的 XML 数据。 要完成此任务,首先要创建一个 DataSet 对象。 然后通过 ReadXml 方法从 XML 文件装载此数据集,并且把报表中两个已有的标签绑定到数据。

C#CopyCode image复制代码
using System;
using System.Data;
using System.Windows.Forms;
// ...

private void button1_Click(object sender, EventArgs e) {
    // Create a report.
    XtraReport1 report = new XtraReport1();

    // Create a DataSet and fill it with data from an XML file.
    DataSet xmlDataSet = new DataSet();
    xmlDataSet.ReadXml(@"..\..\Cars.xml");

    // Bind the report to the DataSet.
    report.DataSource = xmlDataSet;

    // Add two bound labels to the Detail bands.
    report.lbModel.DataBindings.Add("Text", report.DataSource, 
        "Cars.Model", "Model: {0}");
    report.lbTrademark.DataBindings.Add("Text", report.DataSource, 
        "Cars.Trademark", "Trademark: {0}");

    // Show the report's print preview.
    report.ShowPreview();
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Data
Imports System.Windows.Forms
' ...

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 DataSet and fill it with data from an XML file.
    Dim xmlDataSet As New DataSet()
    xmlDataSet.ReadXml("..\..\Cars.xml")

    ' Bind the report to the DataSet.
    report.DataSource = xmlDataSet

    ' Add two bound labels to the Detail bands.
    report.lbModel.DataBindings.Add("Text", report.DataSource, _
        "Cars.Model", "Model: {0}")
    report.lbTrademark.DataBindings.Add("Text", report.DataSource, _
        "Cars.Trademark", "Trademark: {0}")

    ' Show the report's print preview.
    report.ShowPreview()
End Sub

CodeCentralShow Me

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

Expand image参阅