打赏

报表加载数据通用类

 

 

#region 报表加载数据通用类
        public class LoadReportData
        {
            /// <summary>
            /// 记录对应关系的结构
            /// </summary>
            private struct MatchFieldPairType
            {
                public IGRField grField;
                public int MatchColumnIndex;
            }

            /// <summary>
            /// 将 DataReader 的数据转储到 Grid++Report 的数据集中
            /// </summary>
            /// <param name="Report"></param>
            /// <param name="dr"></param>
            public static void FillRecordToReport(IGridppReport Report, IDataReader dr)
            {
                MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, dr.FieldCount)];

                //根据字段名称与列名称进行匹配,建立DataReader字段与Grid++Report记录集的字段之间的对应关系
                int MatchFieldCount = 0;
                for (int i = 0; i < dr.FieldCount; ++i)
                {
                    foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                    {
                        if (String.Compare(fld.RunningDBField, dr.GetName(i), true) == 0)
                        {
                            MatchFieldPairs[MatchFieldCount].grField = fld;
                            MatchFieldPairs[MatchFieldCount].MatchColumnIndex = i;
                            ++MatchFieldCount;
                            break;
                        }
                    }
                }

                // 将 DataReader 中的每一条记录转储到Grid++Report 的数据集中去
                while (dr.Read())
                {
                    Report.DetailGrid.Recordset.Append();

                    for (int i = 0; i < MatchFieldCount; ++i)
                    {
                        if (!dr.IsDBNull(MatchFieldPairs[i].MatchColumnIndex))
                            MatchFieldPairs[i].grField.Value = dr.GetValue(MatchFieldPairs[i].MatchColumnIndex);
                    }

                    Report.DetailGrid.Recordset.Post();
                }
            }

            /// <summary>
            /// 将 DataTable 的数据转储到 Grid++Report 的数据集中
            /// </summary>
            /// <param name="Report"></param>
            /// <param name="dt"></param>
            public static void FillRecordToReport(IGridppReport Report, DataTable dt)
            {
                MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, dt.Columns.Count)];

                //根据字段名称与列名称进行匹配,建立DataTable字段与Grid++Report记录集的字段之间的对应关系
                int MatchFieldCount = 0;
                for (int i = 0; i < dt.Columns.Count; ++i)
                {
                    foreach (IGRField fld in Report.DetailGrid.Recordset.Fields)
                    {
                        if (String.Compare(fld.Name, dt.Columns[i].ColumnName, true) == 0)
                        {
                            MatchFieldPairs[MatchFieldCount].grField = fld;
                            MatchFieldPairs[MatchFieldCount].MatchColumnIndex = i;
                            ++MatchFieldCount;
                            break;
                        }
                    }
                }


                // 将 DataTable 中的每一条记录转储到 Grid++Report 的数据集中去
                foreach (DataRow dr in dt.Rows)
                {
                    Report.DetailGrid.Recordset.Append();
                    for (int i = 0; i < MatchFieldCount; ++i)
                    {
                        if (!dr.IsNull(MatchFieldPairs[i].MatchColumnIndex))
                            MatchFieldPairs[i].grField.Value = dr[MatchFieldPairs[i].MatchColumnIndex];
                    }
                    Report.DetailGrid.Recordset.Post();
                }
            }
        }
        #endregion

posted @ 2011-11-19 10:15  loring  阅读(529)  评论(3编辑  收藏  举报