这个示例展示了在运行时刻如何对报表数据分组。
在本例中,我们把报表绑定到 Northwind 示例数据库 (位于 DevExpress 演示 安装目录中的 nwind.mdb 文件) 的 "Products" 表。
此报表包含 DetailBand 和 GroupHeaderBand 带区。 按照 "Products" 数据表中的 "CategoryID" 数据字段对 GroupHeaderBand 中的数据分组。
注意 |
---|
在运行时刻,数据分组只能在报表文档被创建之前完成。 使用 XRControl.BeforePrint 事件,正好在报表正在被打印/预览之前对数据分组。 |
别忘了把所有 必需的程序集 添加到项目的“引用”列表中。
C# | 复制代码 |
---|---|
using System; using System.Data; using System.Data.OleDb; using System.Windows.Forms; using DevExpress.XtraReports.UI; // ... public XtraReport CreateDataGroupingReport() { // Create a report. XtraReport report = new XtraReport(); // Create a data connection and data adapter. OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\nwind.mdb"); OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Products", connection); // Creata a dataset and fill it. DataSet dataSet1 = new DataSet(); adapter.Fill(dataSet1, "Products"); // Assign the data source to the report. report.DataSource = dataSet1; // Create a detail band and add it to the report. DetailBand detail = new DetailBand(); report.Bands.Add(detail); // Create a group header band and add it to the report. GroupHeaderBand ghBand = new GroupHeaderBand(); report.Bands.Add(ghBand); // Create a group field, // and assign it to the group header band. GroupField groupField = new GroupField("CategoryID"); ghBand.GroupFields.Add(groupField); // Create bound labels, and add them to the report's bands. XRLabel labelGroup = new XRLabel(); labelGroup.DataBindings.Add("Text", report.DataSource, "CategoryID"); ghBand.Controls.Add(labelGroup); XRLabel labelDetail = new XRLabel(); labelDetail.DataBindings.Add("Text", report.DataSource, "ProductName"); detail.Controls.Add(labelDetail); return report; } private void button1_Click(object sender, EventArgs e) { // Create a grouping report and show its print preview. XtraReport report = CreateDataGroupingReport(); report.ShowPreview(); } |
Visual Basic | 复制代码 |
---|---|
Imports System Imports System.Data Imports System.Windows.Forms Imports DevExpress.XtraReports.UI Imports System.Data.OleDb ' ... Public Function CreateDataGroupingReport() As XtraReport ' Create a report. Dim report As New XtraReport() ' Create a data connection and data adapter. Dim connection As New OleDbConnection _ ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\nwind.mdb") Dim adapter As New OleDbDataAdapter("SELECT * FROM Products", connection) ' Creata a dataset and fill it. Dim dataSet1 As New DataSet() adapter.Fill(dataSet1, "Products") ' Assign the data source to the report. report.DataSource = dataSet1 ' Create a detail band and add it to the report. Dim detail As New DetailBand() report.Bands.Add(detail) ' Create a group header band and add it to the report. Dim ghBand As New GroupHeaderBand() report.Bands.Add(ghBand) ' Create a group field, ' and assign it to the group header band. Dim groupField As New GroupField("CategoryID") ghBand.GroupFields.Add(groupField) ' Create bound labels, and add them to the report's bands. Dim labelGroup As New XRLabel() labelGroup.DataBindings.Add("Text", report.DataSource, "CategoryID") ghBand.Controls.Add(labelGroup) Dim labelDetail As New XRLabel() labelDetail.DataBindings.Add("Text", report.DataSource, "ProductName") detail.Controls.Add(labelDetail) Return report End Function Private Sub button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles button1.Click ' Create a grouping report and show its print preview. Dim report As XtraReport = CreateDataGroupingReport() report.ShowPreview() End Sub |
Show Me |
---|
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E1650。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。 |