Asp.Net 中Report Service (RDLC)动态绑定数据-学习笔记
1)托拽ReportViewer控件到aspx页面,此时,系统会自动添加相关引用,修改Web.config设置;
2)修改ReportViewer属性,添加对报表文件的引用:<LocalReport ReportPath="Report.rdlc"></LocalReport>
页面代码:
1
<
form
id
="form1"
runat
="server"
>
2 < div >
3 < rsweb:ReportViewer ID ="ReportViewer1" runat ="server" Height ="703px" Width ="845px" >
4 < LocalReport ReportPath ="Report.rdlc" ></ LocalReport >
5 </ rsweb:ReportViewer >
6 </ div >
7 </ form >
2 < div >
3 < rsweb:ReportViewer ID ="ReportViewer1" runat ="server" Height ="703px" Width ="845px" >
4 < LocalReport ReportPath ="Report.rdlc" ></ LocalReport >
5 </ rsweb:ReportViewer >
6 </ div >
7 </ form >
3)新建数据格式文件(.XSD文件),并在xsd文件中新建一个DataTable
4)新建RDLC报表文件,根据我们的xsd文件格式进行报表设计
5)程序中动态绑定数据(dataSource)和参数(Parameter)
代码:
CS:
1
public
partial
class
_Default : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 LoadReport();
6 }
7 private void LoadReport()
8 {
9 this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
10
11 ReportViewer1.LocalReport.EnableHyperlinks = true; // if there is URL links in your RDLC, this is need
12 ReportViewer1.LocalReport.DataSources.Clear();
13
14 // SetParameters
15 List<ReportParameter> paras = new List<ReportParameter>();
16 paras.Add(new ReportParameter("Name1", "Outer parameter"));
17 this.ReportViewer1.LocalReport.SetParameters(paras);
18
19 // Generate data automatically
20 ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_T1", GetTableT1()));
21
22 ReportViewer1.LocalReport.Refresh();
23 }
24 private DataTable GetTableT1()
25 {
26 DataTable dt = new DataTable();
27 dt.Columns.Add("Key", typeof(string));
28 dt.Columns.Add("Group1", typeof(string));
29 dt.Columns.Add("Group2", typeof(string));
30 dt.Columns.Add("Value", typeof(double));
31
32 dt.Rows.Add("K1", "G1", "M1", 34);
33 dt.Rows.Add("K1", "G1", "M2", 22);
34
35 dt.Rows.Add("K1", "G2", "M1", 76);
36 dt.Rows.Add("K1", "G2", "M2", 55);
37
38 dt.Rows.Add("K2", "G6", "M1", 155);
39 dt.Rows.Add("K2", "G6", "M2", 715);
40
41 dt.Rows.Add("K2", "G7", "M1", 535);
42 dt.Rows.Add("K2", "G7", "M2", 554);
43
44 return dt;
45 }
46}
47
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 LoadReport();
6 }
7 private void LoadReport()
8 {
9 this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
10
11 ReportViewer1.LocalReport.EnableHyperlinks = true; // if there is URL links in your RDLC, this is need
12 ReportViewer1.LocalReport.DataSources.Clear();
13
14 // SetParameters
15 List<ReportParameter> paras = new List<ReportParameter>();
16 paras.Add(new ReportParameter("Name1", "Outer parameter"));
17 this.ReportViewer1.LocalReport.SetParameters(paras);
18
19 // Generate data automatically
20 ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_T1", GetTableT1()));
21
22 ReportViewer1.LocalReport.Refresh();
23 }
24 private DataTable GetTableT1()
25 {
26 DataTable dt = new DataTable();
27 dt.Columns.Add("Key", typeof(string));
28 dt.Columns.Add("Group1", typeof(string));
29 dt.Columns.Add("Group2", typeof(string));
30 dt.Columns.Add("Value", typeof(double));
31
32 dt.Rows.Add("K1", "G1", "M1", 34);
33 dt.Rows.Add("K1", "G1", "M2", 22);
34
35 dt.Rows.Add("K1", "G2", "M1", 76);
36 dt.Rows.Add("K1", "G2", "M2", 55);
37
38 dt.Rows.Add("K2", "G6", "M1", 155);
39 dt.Rows.Add("K2", "G6", "M2", 715);
40
41 dt.Rows.Add("K2", "G7", "M1", 535);
42 dt.Rows.Add("K2", "G7", "M2", 554);
43
44 return dt;
45 }
46}
47
页面:
1
<
body
>
2 < form id ="form1" runat ="server" >
3 < div >
4 < rsweb:ReportViewer ID ="ReportViewer1" runat ="server" Height ="703px" Width ="845px" >
5 < LocalReport ReportPath ="Report.rdlc" ></ LocalReport >
6 </ rsweb:ReportViewer >
7
8
9 </ div >
10 </ form >
11 </ body >
2 < form id ="form1" runat ="server" >
3 < div >
4 < rsweb:ReportViewer ID ="ReportViewer1" runat ="server" Height ="703px" Width ="845px" >
5 < LocalReport ReportPath ="Report.rdlc" ></ LocalReport >
6 </ rsweb:ReportViewer >
7
8
9 </ div >
10 </ form >
11 </ body >
本文转自Jack Niu博客园博客,原文链接:http://www.cnblogs.com/skywind/archive/2007/09/11/889227.html,如需转载请自行联系原作者