锐浪报表加载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

               

猜你喜欢

转载自blog.csdn.net/wohingradio88/article/details/83743393
今日推荐