温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

DevExpress中如何使用Report-XRTable绑定数据

发布时间:2021-07-30 13:58:03 来源:亿速云 阅读:492 作者:Leah 栏目:大数据

本篇文章给大家分享的是有关DevExpress中如何使用Report-XRTable绑定数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

将从跳转前的页面(A)中获取传入的数据(dtOrd、BatchID、ModelID),绑定到Report报表对应的控件 ,代码如下:

 this.xrtBatchID.Text = sBatchID;
                this.xrtModel.Text = sModelID;
                this.xrtDateTime.Text = DateTime.Now.ToShortDateString().ToString();    // 20018-9-4
                if (dtOrd.Rows.Count > 0)
                {
                    

                    XRTable table = new XRTable();
                    
                    for (int i = 0; i < dtOrd.Rows.Count; i++)
                    {
                        if (list[i]!=null) {
                        XRTableRow row = new XRTableRow();
                        XRTableCell TESTING_ITEM = new XRTableCell();
                        XRTableCell TESTING_RESULT = new XRTableCell();
                        XRTableCell TESTING_SPECIFICATION = new XRTableCell();
                        XRTableCell DIMENS = new XRTableCell();
                        XRTableCell TESTING_METHOD = new XRTableCell();

                        row.Cells.Add(TESTING_ITEM);
                        row.Cells.Add(TESTING_RESULT);
                        row.Cells.Add(TESTING_SPECIFICATION);
                        row.Cells.Add(DIMENS);
                        row.Cells.Add(TESTING_METHOD);
                        //TESTING_ITEM.WidthF = 140.14f;
                        //TESTING_RESULT.WidthF = 140.14f;
                        //TESTING_SPECIFICATION.WidthF = 140.14f;
                        //DIMENS.WidthF = 140.14f;
                        //TESTING_METHOD.WidthF = 140.14f;
                        TESTING_ITEM.DataBindings.Add("Text", dtOrd, "TESTING_ITEM");
                        TESTING_RESULT.DataBindings.Add("Text",dtOrd, "TESTING_RESULT");
                        TESTING_SPECIFICATION.DataBindings.Add("Text", dtOrd, "TESTING_SPECIFICATION");
                        DIMENS.DataBindings.Add("Text", dtOrd, "DIMENS");
                        TESTING_METHOD.DataBindings.Add("Text", dtOrd, "TESTING_METHOD");
                        xrTable7.Rows.Add(row);
                        }
                    }
                    
                    DetailBand detailBand = new DetailBand();
                    detailBand.Controls.Add(table);
                    report.Bands.Add(detailBand);
                   

                }

但是这样绑定的结果是(传入数据有两行数据,但绑定显示的都是第一行的内容):

DevExpress中如何使用Report-XRTable绑定数据

于是,将上述代码改进,把传入的Table的多行数据拆分成多个表,分别进行绑定(相信有更简单的,可以直接一次性绑定全部数据的方法,如果有大神看到,请指教一下,多谢),代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using TRSCore;
using System.Text;
using MESCore;
using DevExpress.DataAccess.EntityFramework;
using System.Collections.Generic;

namespace QCMCore.Reports
{
    public partial class rptQCMCOACard : DevExpress.XtraReports.UI.XtraReport
    {
        public rptQCMCOACard()
        {
            InitializeComponent();
        }
        public rptQCMCOACard(DataTable dt,string sBatchID,string sModelID)
        {
            InitializeComponent();
            this.dtOrd = dt;
            this.sBatchID = sBatchID;
            this.sModelID = sModelID;

            
        }
        DataTable dtOrd = null;
        string sBatchID = "";
        string sModelID = "";
        private bool FillHeader()
        {
            try
            {
                if (dtOrd == null)
                {
                    CommonFunction.ShowMsgBox("请先选择需要打印的ITEM!");
                    return false;
                }
                this.xrtBatchID.Text = sBatchID;
                this.xrtModel.Text = sModelID;
                this.xrtDateTime.Text = DateTime.Now.ToShortDateString().ToString();    // 20018-9-4
                if (dtOrd.Rows.Count > 0)
                {
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dtOrd);
                    XtraReport report = new XtraReport();
                    report.DataSource = ds;
                    report.DataMember = "queryProducts";
                    List<DataTable> list = new List<DataTable>();
                    for (int i = 0; i < dtOrd.Rows.Count;i++) {

                        DataTable dt = new DataTable();
                        dt.Columns.Add("ITEM_SEQ");
                        dt.Columns.Add("TESTING_ITEM");
                        dt.Columns.Add("TESTING_RESULT");
                        dt.Columns.Add("TESTING_SPECIFICATION");
                        dt.Columns.Add("DIMENS");
                        dt.Columns.Add("TESTING_METHOD");

                        dt.Rows.Add(dtOrd.Rows[i].ItemArray);
                        list.Add(dt);
                    }

                    XRTable table = new XRTable();
                    
                    for (int i = 0; i < dtOrd.Rows.Count; i++)
                    {
                        if (list[i]!=null) {
                        XRTableRow row = new XRTableRow();
                        XRTableCell TESTING_ITEM = new XRTableCell();
                        XRTableCell TESTING_RESULT = new XRTableCell();
                        XRTableCell TESTING_SPECIFICATION = new XRTableCell();
                        XRTableCell DIMENS = new XRTableCell();
                        XRTableCell TESTING_METHOD = new XRTableCell();

                        row.Cells.Add(TESTING_ITEM);
                        row.Cells.Add(TESTING_RESULT);
                        row.Cells.Add(TESTING_SPECIFICATION);
                        row.Cells.Add(DIMENS);
                        row.Cells.Add(TESTING_METHOD);
                        //TESTING_ITEM.WidthF = 140.14f;
                        //TESTING_RESULT.WidthF = 140.14f;
                        //TESTING_SPECIFICATION.WidthF = 140.14f;
                        //DIMENS.WidthF = 140.14f;
                        //TESTING_METHOD.WidthF = 140.14f;
                        TESTING_ITEM.DataBindings.Add("Text", list[i], "TESTING_ITEM");
                        TESTING_RESULT.DataBindings.Add("Text", list[i], "TESTING_RESULT");
                        TESTING_SPECIFICATION.DataBindings.Add("Text", list[i], "TESTING_SPECIFICATION");
                        DIMENS.DataBindings.Add("Text", list[i], "DIMENS");
                        TESTING_METHOD.DataBindings.Add("Text", list[i], "TESTING_METHOD");
                        xrTable7.Rows.Add(row);
                        }
                    }
                    
                    DetailBand detailBand = new DetailBand();
                    detailBand.Controls.Add(table);
                    report.Bands.Add(detailBand);
                   

                }

                return true;
            }
            catch (Exception ex)
            {
                CommonFunction.ShowMsgBox(ex.Message);
                return false;
            }
        }
        private void ReportHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            FillHeader();
        }

       
    }
}

显示结果如下:

DevExpress中如何使用Report-XRTable绑定数据

另:Report绑定图片:从工具箱拖入xrPictureBox,设置imageUrl载入图片,调整位置和大小即可。

Table下方内容会随着Table数据的多少进行下移(如果数据多的话),所以不用担心会被覆盖。

其他测试过的绑定xrTable的方法(但是都行不通,最多只能绑定一行数据),代码如下:

   private bool FillHeader()
        {
            try
            {
                if (dtOrd == null)
                {
                    CommonFunction.ShowMsgBox("请先选择需要打印的ITEM!");
                    return false;
                }
                this.xrtBatchID.Text = sBatchID;
                this.xrtModel.Text = sModelID;
                this.xrtDateTime.Text = DateTime.Now.ToShortDateString().ToString();    // 20018-9-4
                if (dtOrd.Rows.Count > 0)
                {
                    DataSet ds = new DataSet();
                    ds = GetTempDataSet();
                    if (ds != null)
                    {
                        SetDataBind(ds);
                    }
                    DataRow drPur = dtOrd.Rows[0];
                    string sheetID = string.Empty;
                    if (drPur == null) return false;
                    //绑定采购单信息
                    BindFormData(drPur);
                    //获取采购单ID
                    //sheetID = drPur["ID"].ToString();
                    //获取采购单明细数据集
                    DataSet dsDetail = new DataSet();
                    dsDetail.Tables.Add(dtOrd);
                    //绑定采购单明细信息
                    BindTableData(dsDetail);
                   
                }

                return true;
            }
            catch (Exception ex)
            {
                CommonFunction.ShowMsgBox(ex.Message);
                return false;
            }
        }

 private void BindTableData(DataSet ds)
        {
            
            this.xrTableCell11.DataBindings.Add("Text", ds, "TESTING_ITEM");
            this.xrTableCell12.DataBindings.Add("Text", ds, "TESTING_RESULT");
            this.xrTableCell13.DataBindings.Add("Text", ds, "TESTING_SPECIFICATION");
            this.xrTableCell14.DataBindings.Add("Text", ds, "DIMENS");
            this.xrTableCell15.DataBindings.Add("Text", ds, "TESTING_METHOD");
        }
        private void SetDataBind(DataSet  ds)//绑定数据源
        { 
            this.xrTableCell11.DataBindings.Add("Text",ds, "TESTING_ITEM");
            this.xrTableCell12.DataBindings.Add("Text",ds, "TESTING_RESULT"); 
            this.xrTableCell13.DataBindings.Add("Text",ds, "TESTING_SPECIFICATION"); 
            this.xrTableCell14.DataBindings.Add("Text",ds, "DIMENS"); 
            this.xrTableCell15.DataBindings.Add("Text",ds, "TESTING_METHOD"); 
        }
        /// <summary>
        /// 绑定采购单明细信息
        /// </summary>
        private void BindFormData(DataRow dr)
        {
            //DataSet ds = DataSetOperator.DataRowToDataSet(dr);
            //XRLabel绑定数据 方法1:
            this.xrTableCell11.Text = dr["TESTING_ITEM"].ToString();
            this.xrTableCell12.Text = dr["TESTING_RESULT"].ToString();
            this.xrTableCell13.Text = dr["TESTING_SPECIFICATION"].ToString();
            this.xrTableCell14.Text = dr["DIMENS"].ToString();
            this.xrTableCell15.Text = dr["TESTING_METHOD"].ToString();
            //XRLabel绑定数据 方法2:
            //this.txtDB0337A.DataBindings.Add(new XRBinding("Text", ds, "DB0337A", "{0:yyyy-MM-dd}"));
            //this.txtDB0005A.Text = dr["DB0005A"].ToString();
            //this.txtDB0339A.Text = dr["DB0339A"].ToString();
            //this.txtDB0345A.DataBindings.Add(new XRBinding("Text", ds, "DB0345A", "{0:n2}"));
            //this.labPrintTime.Text = DateTime.Now.Date.ToString();

        }
        private void ReportHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            FillHeader();
        }


        private DataSet GetTempDataSet()
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable("tempTable");
            dt.Columns.Add("TESTING_ITEM");
            dt.Columns.Add("TESTING_RESULT");
            dt.Columns.Add("TESTING_SPECIFICATION");
            dt.Columns.Add("DIMENS");
            dt.Columns.Add("TESTING_METHOD");
            DataRow tempRow;
            if (dtOrd != null)
            {
                for (int i = 0; i < dtOrd.Rows.Count; i++)//i:Row
                {
                    tempRow = dt.NewRow();
                    tempRow[i] = dtOrd.Rows[i];
                    dt.Rows.Add(tempRow);
                }
                ds.Tables.Add(dt);
            }
           
            return ds;
        }

以上就是DevExpress中如何使用Report-XRTable绑定数据,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI