下面的示例演示了如何在运行时刻创建新空白报表 (XtraReport 类的实例),如何创建被绑定到 MDB 文件的数据对象 (通过 XtraReportBase.DataSourceXtraReportBase.DataAdapterXtraReportBase.DataMember 属性),然后填充报表的 DetailBand(细节带区),此带区包含一个显示 MDB 文件中数据的 XRLabel 报表控件。

CodeCentralShow Me

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

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

private void button1_Click(object sender, EventArgs e) {
    // Create an empty report.
    XtraReport report = new XtraReport();

    // Create data objects.
    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Categories",
        @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\nwind.mdb");
    DataSet ds = new DataSet();
    adapter.FillSchema(ds, SchemaType.Source);

    // Bind the report to data.
    report.DataSource = ds;
    report.DataAdapter = adapter;
    report.DataMember = "Table";
    
    // Add a detail band to the report.
    DetailBand detailBand = new DetailBand();
    detailBand.Height = 50;
    report.Bands.Add(detailBand);

    // Add a label to the detail band.
    XRLabel label = new XRLabel();
    label.DataBindings.Add("Text", null, "CategoryName");
    detailBand.Controls.Add(label);

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

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click
    ' Create an empty report.
    Dim report As New XtraReport()

    ' Create data objects.
    Dim adapter As New OleDbDataAdapter("SELECT * FROM Categories", _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\nwind.mdb")
    Dim ds As New DataSet()
    adapter.FillSchema(ds, SchemaType.Source)

    ' Bind the report to data.
    report.DataSource = ds
    report.DataAdapter = adapter
    report.DataMember = "Table"

    ' Add a detail band to the report.
    Dim detailBand As New DetailBand()
    detailBand.Height = 50
    report.Bands.Add(detailBand)

    ' Add a label to the detail band.
    Dim label As New XRLabel()
    label.DataBindings.Add("Text", Nothing, "CategoryName")
    detailBand.Controls.Add(label)

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

Expand image参阅