网上看了好几个方法,要么就是用报表直接连数据库的,另外一个哥们,写的是用另外一个事件绑定数据源的,那样的问题就是,还要另外考虑报表对象怎么传递
后面调整思路,想了想,调整了下代码。改进了一下,主要就是记录一下,另外呢,放出来看看还有什么不对的,需要改进。
1 private void GridPrintPreview(string rptPath, Dictionary<string, object> dic, DataTable dt = null) 2 { 3 GridppReport report = new GridppReport(); 4 report.LoadFromFile(rptPath); 5 if (dic.Count > 0) 6 { 7 foreach (string k in dic.Keys) 8 report.ParameterByName(k).AsString = dic[k].ToString(); 9 } 10 if (dt != null && dt.Rows.Count > 0) 11 { 12 report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(() => 13 { 14 Dictionary<int, int> columnDic = new Dictionary<int, int>(); 15 //Report.DetailGrid.Recordset.Fields的索引从1开始 16 int fieldCount = report.DetailGrid.Recordset.Fields.Count; 17 for (int x = 0; x < fieldCount; x++) 18 { 19 string columnName = report.DetailGrid.Recordset.Fields[x + 1].Name; 20 for (int y = 0; y < dt.Columns.Count; y++) 21 { 22 if (columnName == dt.Columns[y].ColumnName) 23 { 24 columnDic.Add(x, y); 25 break; 26 } 27 } 28 } 29 for (int x = 0; x < dt.Rows.Count; x++) 30 { 31 report.DetailGrid.Recordset.Append(); 32 for (int y = 0; y < columnDic.Count; y++) 33 { 34 report.DetailGrid.Recordset.Fields[y + 1].GetDisplayTextScript = "";//不设置这个值会报错 35 report.DetailGrid.Recordset.Fields[y + 1].Value = dt.Rows[x][columnDic[y]]; 36 } 37 report.DetailGrid.Recordset.Post(); 38 } 39 }); 40 } 41 report.PrintPreview(true); 42 }