溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

DevExpress中如何使用Report-XRTable綁定數(shù)據(jù)

發(fā)布時(shí)間:2021-07-30 13:58:03 來(lái)源:億速云 閱讀:472 作者:Leah 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關(guān)DevExpress中如何使用Report-XRTable綁定數(shù)據(jù),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

將從跳轉(zhuǎn)前的頁(yè)面(A)中獲取傳入的數(shù)據(jù)(dtOrd、BatchID、ModelID),綁定到Report報(bào)表對(duì)應(yīng)的控件 ,代碼如下:

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

                }

但是這樣綁定的結(jié)果是(傳入數(shù)據(jù)有兩行數(shù)據(jù),但綁定顯示的都是第一行的內(nèi)容):

DevExpress中如何使用Report-XRTable綁定數(shù)據(jù)

于是,將上述代碼改進(jìn),把傳入的Table的多行數(shù)據(jù)拆分成多個(gè)表,分別進(jìn)行綁定(相信有更簡(jiǎn)單的,可以直接一次性綁定全部數(shù)據(jù)的方法,如果有大神看到,請(qǐng)指教一下,多謝),代碼如下:

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("請(qǐng)先選擇需要打印的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();
        }

       
    }
}

顯示結(jié)果如下:

DevExpress中如何使用Report-XRTable綁定數(shù)據(jù)

另:Report綁定圖片:從工具箱拖入xrPictureBox,設(shè)置imageUrl載入圖片,調(diào)整位置和大小即可。

Table下方內(nèi)容會(huì)隨著Table數(shù)據(jù)的多少進(jìn)行下移(如果數(shù)據(jù)多的話),所以不用擔(dān)心會(huì)被覆蓋。

其他測(cè)試過(guò)的綁定xrTable的方法(但是都行不通,最多只能綁定一行數(shù)據(jù)),代碼如下:

   private bool FillHeader()
        {
            try
            {
                if (dtOrd == null)
                {
                    CommonFunction.ShowMsgBox("請(qǐng)先選擇需要打印的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;
                    //綁定采購(gòu)單信息
                    BindFormData(drPur);
                    //獲取采購(gòu)單ID
                    //sheetID = drPur["ID"].ToString();
                    //獲取采購(gòu)單明細(xì)數(shù)據(jù)集
                    DataSet dsDetail = new DataSet();
                    dsDetail.Tables.Add(dtOrd);
                    //綁定采購(gòu)單明細(xì)信息
                    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)//綁定數(shù)據(jù)源
        { 
            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>
        /// 綁定采購(gòu)單明細(xì)信息
        /// </summary>
        private void BindFormData(DataRow dr)
        {
            //DataSet ds = DataSetOperator.DataRowToDataSet(dr);
            //XRLabel綁定數(shù)據(jù) 方法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綁定數(shù)據(jù) 方法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綁定數(shù)據(jù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI