这个示例展示了如何使用 XRPivotGrid 控件以程序方式创建一个 交叉表 报表。 有关设计时刻的示例,请参阅 如何: 创建交叉表报表。 要获得一般的说明,请参阅 交叉表报表。
在继续本示例之前,要把所有 必需的程序集 添加到项目的 引用 列表中,并且把一个按钮拖放到窗体上。 然后,以下列方式接管此按钮的 Click 事件。
C# | 复制代码 |
---|---|
using System; using System.Data; using System.Data.OleDb; using System.Windows.Forms; using DevExpress.XtraPivotGrid; using DevExpress.XtraReports.UI; using DevExpress.XtraReports.UI.PivotGrid; // ... private void button1_Click(object sender, EventArgs e) { // Create a cross-tab report. XtraReport report = CreateReport(); // Show its Print Preview. report.ShowPreview(); } private XtraReport CreateReport() { // Create a blank report. XtraReport rep = new XtraReport(); // Create a detail band and add it to the report. DetailBand detail = new DetailBand(); rep.Bands.Add(detail); // Create a pivot grid and add it to the Detail band. XRPivotGrid pivotGrid = new XRPivotGrid(); detail.Controls.Add(pivotGrid); // Create a data connection. OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\nwind.mdb"); // Create a data adapter. OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT CategoryName, ProductName, Country, [Sales Person], Quantity, [Extended Price] FROM SalesPerson", connection); // Creata a dataset and fill it. DataSet dataSet1 = new DataSet(); adapter.Fill(dataSet1, "SalesPerson"); // Bind the pivot grid to data. pivotGrid.DataSource = dataSet1; pivotGrid.DataMember = "SalesPerson"; // Generate pivot grid's fields. XRPivotGridField fieldCategoryName = new XRPivotGridField("CategoryName", PivotArea.RowArea); XRPivotGridField fieldProductName = new XRPivotGridField("ProductName", PivotArea.RowArea); XRPivotGridField fieldCountry = new XRPivotGridField("Country", PivotArea.ColumnArea); XRPivotGridField fieldSalesPerson = new XRPivotGridField("Sales Person", PivotArea.ColumnArea); XRPivotGridField fieldQuantity = new XRPivotGridField("Quantity", PivotArea.DataArea); XRPivotGridField fieldExtendedPrice = new XRPivotGridField("Extended Price", PivotArea.DataArea); // Add these fields to the pivot grid. pivotGrid.Fields.AddRange(new PivotGridField[] {fieldCategoryName, fieldProductName, fieldCountry, fieldSalesPerson, fieldQuantity, fieldExtendedPrice}); return rep; } |
Visual Basic | 复制代码 |
---|---|
Imports System Imports System.Data Imports System.Data.OleDb Imports System.Windows.Forms Imports DevExpress.XtraPivotGrid Imports DevExpress.XtraReports.UI Imports DevExpress.XtraReports.UI.PivotGrid ' ... Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles button1.Click ' Create a cross-tab report. Dim report As XtraReport = CreateReport() ' Show its Print Preview. report.ShowPreview() End Sub Private Function CreateReport() As XtraReport ' Create a blank report. Dim rep As New XtraReport() ' Create a detail band and add it to the report. Dim detail As New DetailBand() rep.Bands.Add(detail) ' Create a pivot grid and add it to the Detail band. Dim pivotGrid As New XRPivotGrid() detail.Controls.Add(pivotGrid) ' Create a data connection. Dim connection As New _ OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\nwind.mdb") ' Create a data adapter. Dim adapter As New _ OleDbDataAdapter("SELECT CategoryName, ProductName, Country, [Sales Person], _ Quantity, [Extended Price] FROM SalesPerson", connection) ' Creata a dataset and fill it. Dim dataSet1 As New DataSet() adapter.Fill(dataSet1, "SalesPerson") ' Bind the pivot grid to data. pivotGrid.DataSource = dataSet1 pivotGrid.DataMember = "SalesPerson" ' Generate pivot grid's fields. Dim fieldCategoryName As New XRPivotGridField("CategoryName", PivotArea.RowArea) Dim fieldProductName As New XRPivotGridField("ProductName", PivotArea.RowArea) Dim fieldCountry As New XRPivotGridField("Country", PivotArea.ColumnArea) Dim fieldSalesPerson As New XRPivotGridField("Sales Person", PivotArea.ColumnArea) Dim fieldQuantity As New XRPivotGridField("Quantity", PivotArea.DataArea) Dim fieldExtendedPrice As New XRPivotGridField("Extended Price", PivotArea.DataArea) ' Add these fields to the pivot grid. pivotGrid.Fields.AddRange(New PivotGridField() {fieldCategoryName, fieldProductName, _ fieldCountry, fieldSalesPerson, fieldQuantity, fieldExtendedPrice}) Return rep End Function |
Show Me |
---|
在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E67。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。 |