您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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); }
但是這樣綁定的結果是(傳入數據有兩行數據,但綁定顯示的都是第一行的內容):
于是,將上述代碼改進,把傳入的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(); } } }
顯示結果如下:
另: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綁定數據,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。