DEV-打印设计2 用户自定义模版配置数据源

1.用户自定义模版 设置标签内对应的数据源。

创建数据源(数据源内的列名客户是可以赋值数据的)

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

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

                    //设置主表和从表的父子关系
                    DataColumn parentColumn = dsRep.Tables["parent"].Columns["ID"];
                    DataColumn childColumn = dsRep.Tables["child"].Columns["ID"];
                    DataRelation R1 = new DataRelation("R1", parentColumn, childColumn);
                    dsRep.Relations.Add(R1);

                    XtraReport mReport = new XtraReport();
                    mReport.LoadLayout(Path.Combine(Directory.GetCurrentDirectory(), mblj)); //报表模板文件 


                    //查找组件 
                    GroupFooterBand GroupFooter1 = mReport.FindControl("GroupFooter1", true) as GroupFooterBand;
                    GroupHeaderBand GroupHeader1 = mReport.FindControl("GroupHeader", true) as GroupHeaderBand;
                    DetailReportBand DetailReport = mReport.FindControl("DetailReport", true) as DetailReportBand;
                    DetailBand detailBand = mReport.FindControl("Detail", true) as DetailBand;
                    DetailBand detail1 = mReport.FindControl("Detail1", true) as DetailBand;

                    //先绑定数据源
                    mReport.DataMember = "parent";
                    mReport.DataSource = dsRep;

                    DetailReport.DataMember = "R1";
                    DetailReport.DataSource = dsRep;

                    if (detailBand != null)
                    {
                        DataBindHeadControl(detailBand.Controls, dsRep);
                    }
                    else
                    {
                        MessageBox.Show("没有默认模板,请放入默认模板到根路径下面文件名(XtraReport1.repx)");
                        return;
                    }

                    if (detail1 != null)
                    {
                        DataBindBodyControl(detail1.Controls, dsRep);
                    }
                    else
                    {
                        MessageBox.Show("没有默认模板,请放入默认模板到根路径下面文件名(XtraReport1.repx)");
                        return;
                    }

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

                    if (System.IO.File.Exists(newPath2))
                    {
                        mReport.PrintingSystem.PageSettings.RestoreFromXml(newPath2);
                    }

      mReport.CreateDocument();
                    mReport.PrintingSystem.ShowMarginsWarning = false;
                    mReport.PrintingSystem.ExportOptions.PrintPreview.DefaultExportFormat = PrintingSystemCommand.ExportXls;
                    mReport.ShowPreview();
                    mReport.PrintingSystem.PageSettings.SaveToXml(newPath2);

---------------------------------------------------------------------

    private void DataBindBodyControl(XRControlCollection control, DataSet dsSet)
        {
            foreach (XRControl c in control)
            {
                if (c is XRPanel)
                {
                    DataBindBodyControl(c.Controls, dsSet);
                }

                if (c is XRTableRow)
                {
                    DataBindBodyControl(c.Controls, dsSet);
                }

                if (c is XRTable)
                {
                    DataBindBodyControl(c.Controls, dsSet);
                }

                if (c is XRLabel)
                {
                    XRLabel txt = c as XRLabel;

                    string tag = (string)txt.Tag;
                    if (!string.IsNullOrEmpty(tag))
                    {
                        if (dsSet.Tables[1].Columns.Contains(tag))
                        {
                            txt.DataBindings.Add("Text", dsSet, "R1." + tag);
                        }
                    }
                }

                if (c is XRTableCell)
                {
                    XRTableCell txt = c as XRTableCell;

                    string tag = (string)txt.Tag;
                    if (!string.IsNullOrEmpty(tag))
                    {
                        if (dsSet.Tables[1].Columns.Contains(tag))
                        {
                            txt.DataBindings.Add("Text", dsSet, "R1." + tag);
                        }
                    }
                }

                if (c is XRPictureBox)
                {
                    XRPictureBox txt = c as XRPictureBox;
                    txt.ImageUrl = newPathlogo;
                }

                if (c is XRBarCode)
                {
                    XRBarCode txt = c as XRBarCode;
                    string tag = (string)txt.Tag;
                    if (!string.IsNullOrEmpty(tag))
                    {
                        if (dsSet.Tables[1].Columns.Contains(tag))
                        {
                            txt.DataBindings.Add("Text", dsSet, "R1." + tag);
                        }
                    }
                }


            }
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值