1. 程式人生 > >銳浪報表載入List泛型資料

銳浪報表載入List泛型資料

個人比較喜歡使用Dapper抓取資料,該ORM抓取到的都是List泛型物件,但銳浪官方的例子都是DataTable載入記錄集,所以我就寫了一個List泛型載入記錄集的方法,供一些和我一樣的小菜參考,有問題可以拍磚,謝謝.

  • List泛型載入銳浪資料集

public class GridReportHelper
    {
        private struct MatchFieldPairType
        {
            public IGRField grField;
            public int MatchColumnIndex;
        }
        //List載入資料集
        public static void FillRecordToReport<T>(IGridppReport Report, List<T> listT)
        {

            Type type = typeof(T);  //反射型別             

            MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, type.GetProperties().Length)];

            //根據欄位名稱與列名稱進行匹配,建立DataReader欄位與Grid++Report記錄集的欄位之間的對應關係
            int MatchFieldCount = 0;
            int i = 0;
            MemberInfo[] members = type.GetMembers();
            foreach (MemberInfo memberInfo in members)
            {
                foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                {
                    if (String.Compare(fld.Name, memberInfo.Name, true) == 0)
                    {
                        MatchFieldPairs[MatchFieldCount].grField = fld;
                        MatchFieldPairs[MatchFieldCount].MatchColumnIndex = i;
                        ++MatchFieldCount;
                        break;
                    }
                }
                ++i;
            }

            Helper.StringHelper stringhelper = new StringHelper();      //定義string幫助類
            // 將 DataTable 中的每一條記錄轉儲到 Grid++Report 的資料集中去
            foreach (T t in listT)
            {
                Report.DetailGrid.Recordset.Append();

                for ( i = 0; i < MatchFieldCount; ++i)
                {
                    if (stringhelper.GetValue<T>(t, MatchFieldPairs[i].grField.Name) != null)
                        MatchFieldPairs[i].grField.Value = stringhelper.GetValue<T>(t, MatchFieldPairs[i].grField.Name);
                }

                Report.DetailGrid.Recordset.Post();
            }
        }
        
    }
  • 第一步: 定義Grid++Report報表主物件 

 private GridppReport Report = new GridppReport();
  •  第二步:載入報表模板 

Report.LoadFromFile(inifilehelper.ReadValue("ZSLablePrint", "ReportPath"));
  • 第三步:載入報表事件              

Report.PrintAsDesignPaper = true;       //列印設定與設計頁面一致
//連線報表取數事件
Report.FetchRecord -= new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
Report.PrintEnd += new _IGridppReportEvents_PrintEndEventHandler(Report_PrintEnd);

                
Report.Printer.DesignPrinterName = inifilehelper.ReadValue("ZSLablePrint", "PrinterName"); //預設印表機名稱
Report.Printer.PaperName = inifilehelper.ReadValue("ZSLablePrint", "PaperName");

簡要說明: 

FetchRecordEventHandler事件需要先刪除後再載入,是避免資料集重複載入的問題,

PrintEndEventHandler是載入列印後事件,例如列印後記錄列印次數。

  • 第四步: 報表預覽

Report.PrintPreview(true);
  • 附記錄載入方法和列印後事件
/// <summary>
/// 動態載入list
/// </summary>
private void ReportFetchRecord()
{
	List<Model.AL_LablesZS> listlableszs = new List<Model.AL_LablesZS>();       
	Model.AL_LablesZS lableszs = new Model.AL_LablesZS(); //定義標籤物件
	string strSQL = @"SELECT * FROM AL_LablesZS WHERE ID = @ID";
	lableszs = Helper.DapperHelper.GetFirstOrDefault<Model.AL_LablesZS>(strSQL, new { ID = id });

	int x = lableszs.FAuxQty;
	int y = lableszs.PackageQty;
	//判斷列印標籤數量,每次列印最多10張,避免沒有更改每包數量的問題
	if (x/y > 10)
	{
		MessageBox.Show("每次標籤列印數量不能超過10張,煩請確定【每包數量】及【生產數量】");
		return;
	}

	int i = 0;              //定義序號變數
	//新增資料集
	while (x>0)
	{
		i = i + 1;
		Model.AL_LablesZS tmp = new Model.AL_LablesZS(); //定義標籤物件
		tmp = Helper.ObjectHelper.TransReflection<Model.AL_LablesZS, Model.AL_LablesZS>(lableszs);
		tmp.FBatch2 = tmp.FBatch2 + i.ToString();       //生產批號2最後一位序號自動增加.
		tmp.GUID = tmp.ID.ToString() + "-" + (100 + i).ToString() + DateTime.Now.ToString("HHmmssfff") + "-" + tmp.CZFBatchNo;

		if (x>y)     //生產數量大於包裝數量
		{
			listlableszs.Add(tmp);     //新增一個物件
			x = x - y;                      //新增完成後減少該包裝數量
			continue;                       //繼續下一迴圈
		}
		else
		{
			tmp.PackageQty = x;        //如果不足包裝數量時,則包裝數量等於剩餘數量
			listlableszs.Add(tmp);
			break;                              //退出迴圈
		}

	}

	//自動填充報表資料集
	Helper.GridReportHelper.FillRecordToReport<Model.AL_LablesZS>(Report, listlableszs);


}
/// <summary>
/// 報表列印結束後事件
/// </summary>
private void Report_PrintEnd()
{         
	Model.AL_LablesZS lableszs = new Model.AL_LablesZS(); //定義標籤物件
	string strSQL = @"UPDATE AL_LablesZS SET PrintUser = @PrintUser,PrintTime = @PrintTime,
								PrintCount = PrintCount + 1 WHERE ID = @ID";
	lableszs.ID = id;       
	lableszs.PrintUser = Helper.DapperHelper.username.UserName;         //當前使用者
	lableszs.PrintTime = DateTime.Now;
	Helper.DapperHelper.ExecuteWithTrans(strSQL, lableszs);

}
#endregion