DevExpress_Report 主从报表绑定数据,分页打印



首先新建一个XtraReport类。根据需要设计报表页面布局;子报表需要添加DetailReport

DevExpress_Report 主从报表绑定数据,分页打印_第1张图片

布局设计完毕后,写代码绑定数据;

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using Zeda.AssistantClass;

namespace LYWJMIS
{
    public partial class MainAndChild : DevExpress.XtraReports.UI.XtraReport
    {
        private DataSet dsSheet;
        private DataSet dsDetail;
        public MainAndChild()
        {
            InitializeComponent();
            this.AfterPrint += new EventHandler(MainAndChild_AfterPrint);
      
        }

        void MainAndChild_AfterPrint(object sender, EventArgs e)
        {
            //this.InsertPageBreaks(this.xrPageBreak1);
        }
        public MainAndChild(DataSet dsSheet, DataSet dsDetail)
            : this()
        {
            this.dsDetail = dsDetail;
            this.dsSheet = dsSheet;

            DataSet dsRep =new DataSet();

            DataTable dtSheet = dsSheet.Tables[0].Copy();
            dtSheet.TableName="parent";
            dsRep.Tables.Add(dtSheet);

            DataTable dtDetail = dsDetail.Tables[0].Copy();
            dtDetail.TableName = "child";
            dsRep.Tables.Add(dtDetail);

            GroupField gf = new GroupField("ID", XRColumnSortOrder.Ascending);
            GroupHeader1.GroupFields.Add(gf);
            //设置主表和从表的父子关系
            DataColumn parentColumn = dsRep.Tables["parent"].Columns["ID"];
            DataColumn childColumn = dsRep.Tables["child"].Columns["DB0025A"];
            DataRelation R1 = new DataRelation("R1", parentColumn, childColumn);
            dsRep.Relations.Add(R1);
            //绑定主表的数据源
            this.DataMember = "parent";
            this.DataSource = dsRep;
            //绑定明细表的数据源
            this.DetailReport.DataMember = "R1";
            this.DetailReport.DataSource = dsRep;
            //this.DetailReport.dat

            BindTableData(dsRep);
            BindFormData(dsRep);

            //在页脚之后设置分页符
            GroupFooter1.PageBreak = PageBreak.AfterBand;

        }
        /// 
        /// 绑定采购单明细信息
        /// 
        private void BindTableData(DataSet ds)
        {
            //为XRTable的每一列绑定数据集及对应的字段
            this.xrTableCell1.DataBindings.Add("Text", ds, "R1.DB0137A");//名称 (DB0137A为字段名称)
            this.xrTableCell2.DataBindings.Add("Text", ds, "R1.DB0152A");//规格
            this.xrTableCell3.DataBindings.Add("Text", ds, "R1.DB0150A");//单位
            this.xrTableCell7.DataBindings.Add("Text", ds, "R1.DB0151A");//产地
            this.xrTableCell8.DataBindings.Add("Text", ds, "R1.DB0168A");//剂型
            this.xrTableCell9.DataBindings.Add("Text", ds, "R1.DB0183A");//计量规格
            this.xrTableCell10.DataBindings.Add("Text", ds, "R1.DB0188A", "{0:n2}");//进价
            this.xrTableCell11.DataBindings.Add("Text", ds, "R1.DB0354A", "{0:f0}");//数量
            //设置本页小计(数量小计),SummaryRunning.Page指定统计范围,只统计本页数量。
            this.xrTableCell23.DataBindings.Add("Text", ds, "R1.DB0354A", "{0:f0}");//数量
            this.xrTableCell23.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Sum, string.Empty);
            //绑定本单合计,SummaryRunning.Group 指定统计范围,统计每一组(一个采购单为一组)数量。
            this.xrLabel7.DataBindings.Add("Text", ds, "R1.DB0354A","{0:f0}");
            this.xrLabel7.Summary = new XRSummary(SummaryRunning.Group,SummaryFunc.Sum,string.Empty);
        }
        /// 
        /// 绑定采购单明细信息
        /// 
        private void BindFormData(DataSet ds)
        {
            //XRLabel绑定数据 方法1:
            this.txtDB0336A.DataBindings.Add("Text",ds,"DB0336A");
            //XRLabel绑定数据 方法2:
            this.txtDB0337A.DataBindings.Add(new XRBinding("Text", ds, "DB0337A", "{0:yyyy-MM-dd}"));
            this.txtDB0005A.DataBindings.Add("Text", ds, "DB0005A");
            this.txtDB0339A.DataBindings.Add("Text", ds, "DB0339A");
            this.txtDB0345A.DataBindings.Add(new XRBinding("Text", ds, "DB0345A", "{0:n2}"));
            this.labPrintTime.Text = DateTime.Now.Date.ToString();

        }
    }
}

打印预览代码

   private void customButton1_Click(object sender, EventArgs e)
        {
            DataSet dsDetail=DataService .Instance .GetPurchaseSheetDetailInfoByDate(this.dtpDate .DStartDate,this.dtpDate .DEndDate);
            MainAndChild rep = new MainAndChild(dsSearch,dsDetail);
            //设置纸张类型为自定义
            rep.PaperKind = System.Drawing.Printing.PaperKind.Custom;
            //设置纸张大小
            double width = 24.1 * 0.3937008 * Dpi.GetDeviceCapsX();
            double height = 9.3 * 0.3937008 * Dpi.GetDeviceCapsY();
            rep.PageSize = new System.Drawing.Size((int)width, (int)height);
            //打印预览
            rep.ShowPreview();
        }

DevExpress_Report 主从报表绑定数据,分页打印_第2张图片


你可能感兴趣的:(C#,DevExpress)