DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表

本文介绍如何使用 DevExpressXtraReports 创建带有透视网格的报表。通过创建空白报表并添加详细信息带区,接着在详细信息带区中添加透视网格控件,并通过连接数据源、设置数据成员及生成字段完成报表的制作。

本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更多的人不会像我这样浪费时间才写的这篇文章,高手不想的看请路过
本文内容来DevExpress
XtraReports帮助文档,如看过类似的请略过。
废话少说
开始正事
在继续本示例之前,要把所有 必需的程序集 添加到项目的 引用
列表中,并且把一个按钮拖放到窗体上。 然后,以下列方式接管此按钮的 Click 事件。

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;
// ...


namespace docXRPivotGrid {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
  
      }

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

 


转载于:https://www.cnblogs.com/Footprints/archive/2013/04/09/3009791.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值