RDLC报表总结

这2天纠结的报表基本上已近完成大部分功能。现在总结一下自己近期的学习成果

首先制作微软RDLC报表由以下三部分构成:1.制作自己的DateSet集合(就是报表的数据集);2.制作自己的报表文件.rdlc文件,用于画做报表样式,里面有微软自带的导出和打印功能,其实就为了少做这2个功能做少部分报表完全还不如自己生成报表灵活。制作显示报表的前台页面aspx文件,基本上就是插入一个ReportViewer然后关联上面的.rdlc文件,注意别忘了更新数据源和插入ScriptManager.

下图是我这是这几天来做的,但基本都是建立这3个文件,网上可以找很多教程可以学习,

这些是我主要参考的网站

http://www.cnblogs.com/JamesLi2015/archive/2010/01/30/1660086.html

http://www.cnblogs.com/lauyee/archive/2010/07/26/1783694.html

http://wenku.baidu.com/view/364f5048c850ad02de8041f5.html

http://www.cnblogs.com/lauyee/archive/2010/07/18/1780124.html

好吧,开始做了,前面都很简单:怎么做一个DataSet,怎么绘制自己的RDLC,这些东西查查什么是数据集,什么是表头,行组,列祖就可以。还有报表里面的参数也可以传入。用LocalReport.SetParameters()方法。

难点发生在要动态构造一个DataSet,网上刚开始给了好多方法,但是自己老是不成功,后来总结出2点

1. 参照的数据集名称错误,假如你的RDLC参照数据集DateSet1,在自动定义新的数据集时候

ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", ds.Tables[0]));,

必须把名字标记成DateSet1,否则会无法识别。还有新的数据集字段必须和原有字段名称一致。

2. 这个最悲剧,竟然是自己的的SQL语句写错了,汗颜不说了...

看看源代码和最后运行图示

[csharp] view plaincopy
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     //先引用源报表并修改参数  
  4.       ReportParameter rp = new ReportParameter("content""库房");  
  5.     this.ReportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "Report4.rdlc";  
  6.     this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });  
  7.   
  8.     string connstring = "Data Source=192.168.1.111;Initial Catalog=SameWayAgile_4_14;Integrated Security=True";  
  9.     System.Data.SqlClient.SqlConnection conn1 = new System.Data.SqlClient.SqlConnection(connstring);  
  10.     System.Data.SqlClient.SqlCommand command1 = new System.Data.SqlClient.SqlCommand(  
  11.     "SELECT GoodsName AS 商品名称, ABCTypeName AS ABC类, MiddleTypeName AS 中类,  GoodsTypeName AS 大类, TotalValue AS 金额, ActualQuantity AS 数量 FROM Part_GoodsView", conn1);  
  12.     System.Data.SqlClient.SqlDataAdapter ada1 = new System.Data.SqlClient.SqlDataAdapter(command1);  
  13.     DataSet ds = new DataSet();  
  14.     try  
  15.     {  
  16.         conn1.Open();  
  17.         ada1.Fill(ds);  
  18.         ds.DataSetName = "DataSet1";  
  19.     }  
  20.     catch (Exception ex)  
  21.     {  
  22.         string ss = ex.Message;  
  23.     }  
  24.   
  25.     finally  
  26.     {  
  27.         conn1.Close();  
  28.         command1.Dispose();  
  29.         conn1.Dispose();  
  30.     }  
  31.   
  32.       
  33.     if (ds.Tables[0] != null)  
  34.     {  
  35.         ReportViewer1.LocalReport.DataSources.Clear();  
  36.         ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", ds.Tables[0]));  
  37.     }  
  38.     ReportViewer1.LocalReport.Refresh();  
  39. }  

今天做了一个变态的报表,刚开始觉得不能实现,后来了解报表文件特性还是可以实现的,就是矩阵里面嵌套矩阵。哈哈

好了插图

原文地址:https://www.cnblogs.com/zhwl/p/3283924.html