DevExpress_Report 主從報表綁定數據,分頁打印


首先新建一個XtraReport類。根據需要設計報表頁面布局;子報表需要添加DetailReport

布局設計完畢后,寫代碼綁定數據;

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;

        }
        /// <summary>
        /// 綁定采購單明細信息
        /// </summary>
        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);
        }
        /// <summary>
        /// 綁定采購單明細信息
        /// </summary>
        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();
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM