锐浪报表应用系列四(说一说你不一定知道的取值方法)

          首先我们来说下上次说的报表模板中,数据库表连接串,sql语句,这个可以直接取到数据然后展示,那么我们可以更近一步呢,这个报表的内容是不确定的,比如说我们打印的出库入库单据等,对吧。这里简单的就是一个参数传值,我们在报表模板里面定义一个参数,然后在sql语句中引用这个参数,最后就是我们在程序里面把我们要的值传给这个参数。这是最简单的一种传值方法,相信初学者开始肯定都是这么干的。

 private void btnDaYin_Click(object sender, EventArgs e)
        {
            a();
            FrmCaiGouRuKuPreview frm = new FrmCaiGouRuKuPreview();
            frm.AttachReport(Report);
            frm.ShowDialog();
        }

        public void a()
        {
            //从对应文件中载入报表模板数据
            if (this.UserInfo.DepartmentName == "a")
            {
                Report.LoadFromFile(BaseBaoBiao.GetReportTemplatePath() + "a.grf");
            }
            else if (this.UserInfo.DepartmentName == "b")
            {
                Report.LoadFromFile(BaseBaoBiao.GetReportTemplatePath() + "b.grf");
            }
            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
            //设置与数据源的连接串,因为在设计时指定的数据库路径是绝对路径。
            if (Report.DetailGrid != null)
                Report.DetailGrid.Recordset.ConnectionString = BaseBaoBiao.GetDatabaseConnectionString();
        }

        private void ReportInitialize()
        {
            //传递参数
            Report.ParameterByName("YuanName").AsString = this.UserInfo.CompanyName;
            Report.ParameterByName("KeShiName").AsString = this.UserInfo.DepartmentName;
            Report.ParameterByName("RuKuFangShi").AsString = "采购入库";
            Report.ParameterByName("RuKuDanHao").AsString = EntityId;
            DateTime riqi = Convert.ToDateTime(grvHead.GetFocusedRowCellValue("CreateOn"));
            Report.ParameterByName("CaiGouRiQi").AsString = riqi.ToString("yyyy年MM月dd日");
            Report.ParameterByName("Id").AsString = grvHead.GetFocusedRowCellValue("Id").ToString();

            string keshiname = grvHead.GetFocusedRowCellValue("SuoShuLeiBieName").ToString();
            //keshiname=keshiname.Remove(0,3);
            Report.ParameterByName("FenLei").AsString = keshiname;
            Report.ParameterByName("GongHuoShang").AsString = grvHead.GetFocusedRowCellValue("GongYingShang").ToString();
        }

           看上面代码,这是我刚开始用的时候自己写的例子,注意其中a方法,加载数据库连接语句和参数。ReportInitialize方法是获取参数。这里获取到之后报表模板里面sql语句where条件 例如:Code=:RuKuDanHao,这个就是在报表模板里面的写法。简单的开始学习了,我们也知道了,那么当我们遇到传值的时候不是一个字符串或者数字呢,这些麻烦来了,因为如果我们传一个类似1,2,3,4,5 这种参数或者'a','b','c','d'这样的参数呢,这个方法就行不通了,怎么办呢。别着急,它既然科传参数,那么是不是也可以传数据集呢,呵呵,当我顺着这条思路去做的时候,结果告诉我,是可以的。那么怎么来实现呢。直接贴代码好了,各位看官看好了。

protected GridppReport Report = new GridppReport(); --这个是必须new的

DataTable dtReport = new DataTable();--这个呢,注意下面就是重点了,上面我们不是已经说好了,我们直接传数据集吗?

-------此处省略dtReport数据集获取

new Reports() .FillRecordToReport(Report, dtReport);--这样我们就把数据集赋值给了报表明细列表。然后我们就可以打开报表展示数据了。

这里有人会问,FillRecordToReport方法怎么写呢。别着急,后面贴上。

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

            //根据字段名称与列名称进行匹配,建立DataReader字段与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;
                    }
                }
            }

         各位看官可以慢慢看哦,这里既然贴出了直接传入数据集那么肯定还会有其他的方法。下面我再贴一段。用法类似,大家可以试试的。

        // 将 DataReader 的数据转储到 Grid++Report 的数据集中
        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;
                    }
                }
            }


            // Loop through the contents of the OleDbDataReader object.
            // 将 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();
            }
        }

        这个应该就是我们现在用的,模板只放明细记录字段,所有的我们都放在了代码里,想怎么玩就怎么玩。只有你想不到,没有做不到的。今天就说到这里,以上内容都是对你有帮助的,如果你是初学者,那么这个肯定就是捷径,如果你是已经在使用的朋友,可以看看是不是跟我的方法一样呢,不一样,您会不会也给说出来,我们共同分享,共同学习呢。如果有疑问的地方可以@我,谢谢。

原文地址:https://www.cnblogs.com/dongni/p/5060903.html