WinForm RDLC SubReport Step by step

最近在做的一个PO管理系统,因为要用到订单打印,没有用水晶报表,直接使用VS2010的Reporting.参考了网上的一些文章,但因为找到的数据是用于WebForm的,适配到WinForm有点区别,竟然花了很久才搞通.所以现在做个Step By Step以记录.

参考Jimmy.Yang的博文:

http://www.cnblogs.com/yjmyzz/archive/2011/09/19/2180940.html

 

开发环境:      VS2010 C#

 

第一步,新建项目

 WinForm RDLC SubReport Step by step

 

2.在项目中新建数据集

WinForm RDLC SubReport Step by step  

3.在数据集DataSet按图标新建表T_DEPT,T_EMP.

  WinForm RDLC SubReport Step by step

4.在项目中新建报表rptDEPT

 WinForm RDLC SubReport Step by step 

5.在报表rptDEPT.rdlc中新增一个Tablix表,选择显示DEPTNO,DEPTNAME.

 WinForm RDLC SubReport Step by step

6. 在Form1里新建一个ReportViewer1.

WinForm RDLC SubReport Step by step

 

WinForm RDLC SubReport Step by step

WinForm RDLC SubReport Step by step

 

并填上如下代码

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

using Microsoft.Reporting.WinForms;

 

namespace WinFormSubReport2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            this.reportViewer1.LocalReport.ReportPath = @"..\..\rptDEPT.rdlc";

            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetDEPT",GetDeptData()));

            this.reportViewer1.RefreshReport();

        }

        private DataTable GetDeptData()

        {

            DataTable dt = new DataTable();

            dt.Columns.Add("DEPTNO", typeof(string));

            dt.Columns.Add("DEPTNAME", typeof(string));

            dt.Rows.Add("01", "办公室");

            dt.Rows.Add("02", "技术部");

            dt.Rows.Add("03", "销售部");

            dt.Rows.Add("04", "客服部");

 

            return dt;

        }

    }

}

  

然后运行结果显示如下:

 WinForm RDLC SubReport Step by step

 

以上完成了一个单报表的制作,下面演示子报表的添加.

 

7.在项目中新建一个rptEMP.rdlc.

 WinForm RDLC SubReport Step by step

 

在子报表中增加表和字段

 WinForm RDLC SubReport Step by step

 

在父报表中添加子报表控件

 WinForm RDLC SubReport Step by step

 

 

在子报表控件上点击右键,选择属性,将rptEMP设置为子报表.

WinForm RDLC SubReport Step by step

WinForm RDLC SubReport Step by step
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 using Microsoft.Reporting.WinForms;
11 
12 namespace WinFormSubReport2
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void Form1_Load(object sender, EventArgs e)
22         {
23             this.reportViewer1.LocalReport.ReportPath = @"..\..\rptDEPT.rdlc";
24             this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetDEPT",GetDeptData()));
25 
26             //定義子報表處理方法
27             this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
28 
29             this.reportViewer1.RefreshReport();
30         }
31         private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
32         {
33             e.DataSources.Add(new ReportDataSource("DataSetEMP", GetEmpData()));
34         }
35         private DataTable GetDeptData()
36         {
37             DataTable dt = new DataTable();
38             dt.Columns.Add("DEPTNO", typeof(string));
39             dt.Columns.Add("DEPTNAME", typeof(string));
40             dt.Rows.Add("01", "辦公室");
41             dt.Rows.Add("02", "技術部");
42             dt.Rows.Add("03", "銷售部");
43             dt.Rows.Add("04", "客服部");
44 
45             return dt;
46         }
47         private DataTable GetEmpData()
48         {
49             DataTable dt = new DataTable();
50             dt.Columns.Add("EMPNO", typeof(string));
51             dt.Columns.Add("EMPNAME", typeof(string));
52             dt.Columns.Add("DEPTNO", typeof(string));
53             dt.Rows.Add("001", "楊過", "01");
54             dt.Rows.Add("002", "令狐沖", "02");
55             dt.Rows.Add("003", "風清揚", "02");
56             dt.Rows.Add("004", "郭靖", "03");
57             dt.Rows.Add("005", "趙敏", "04");
58             return dt;
59         }
60     }
61 }
View Code

此时运行程序,父报表和子报表都显示完整的数据.

 WinForm RDLC SubReport Step by step

 8.在父报表中增加一个参数DeptNo.

 WinForm RDLC SubReport Step by step

 

选中父报表的tablix,在属性栏的Filter项里添加过滤参数

 WinForm RDLC SubReport Step by step

 

在Form1.cs代码里动态增加一个参数.

 WinForm RDLC SubReport Step by step

 

在子报表控件中增加一个[DeptNo]=[@DeptNo],作为子报表的参数

 WinForm RDLC SubReport Step by step

 

在子报表设计窗口增加报表.

 WinForm RDLC SubReport Step by step

 

最终Form1.cs代码:

WinForm RDLC SubReport Step by step
  1 using System;
  2 
  3 using System.Collections.Generic;
  4 
  5 using System.ComponentModel;
  6 
  7 using System.Data;
  8 
  9 using System.Drawing;
 10 
 11 using System.Linq;
 12 
 13 using System.Text;
 14 
 15 using System.Windows.Forms;
 16 
 17  
 18 
 19 using Microsoft.Reporting.WinForms;
 20 
 21  
 22 
 23 namespace WinFormSubReport2
 24 
 25 {
 26 
 27     public partial class Form1 : Form
 28 
 29     {
 30 
 31         public Form1()
 32 
 33         {
 34 
 35             InitializeComponent();
 36 
 37         }
 38 
 39  
 40 
 41         private void Form1_Load(object sender, EventArgs e)
 42 
 43         {
 44 
 45             //指定父报表文件
 46 
 47             this.reportViewer1.LocalReport.ReportPath = @"..\..\rptDEPT.rdlc";
 48 
 49             //给父报表传参数
 50 
 51             this.reportViewer1.LocalReport.SetParameters(new ReportParameter("DeptNo", "02"));
 52 
 53             //给父报表传数据
 54 
 55             this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetDEPT",GetDeptData()));
 56 
 57  
 58 
 59             //定义子报表处理方法
 60 
 61             this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
 62 
 63  
 64 
 65             this.reportViewer1.RefreshReport();
 66 
 67         }
 68 
 69         private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
 70 
 71         {
 72 
 73             e.DataSources.Add(new ReportDataSource("DataSetEMP", GetEmpData()));
 74 
 75         }
 76 
 77         private DataTable GetDeptData()
 78 
 79         {
 80 
 81             DataTable dt = new DataTable();
 82 
 83             dt.Columns.Add("DEPTNO", typeof(string));
 84 
 85             dt.Columns.Add("DEPTNAME", typeof(string));
 86 
 87             dt.Rows.Add("01", "办公室");
 88 
 89             dt.Rows.Add("02", "技术部");
 90 
 91             dt.Rows.Add("03", "销售部");
 92 
 93             dt.Rows.Add("04", "客服部");
 94 
 95  
 96 
 97             return dt;
 98 
 99         }
100 
101         private DataTable GetEmpData()
102 
103         {
104 
105             DataTable dt = new DataTable();
106 
107             dt.Columns.Add("EMPNO", typeof(string));
108 
109             dt.Columns.Add("EMPNAME", typeof(string));
110 
111             dt.Columns.Add("DEPTNO", typeof(string));
112 
113             dt.Rows.Add("001", "杨过", "01");
114 
115             dt.Rows.Add("002", "令狐冲", "02");
116 
117             dt.Rows.Add("003", "风清扬", "02");
118 
119             dt.Rows.Add("004", "郭靖", "03");
120 
121             dt.Rows.Add("005", "赵敏", "04");
122 
123             return dt;
124 
125         }
126 
127     }
128 
129 }
View Code

运行结果如下:

 WinForm RDLC SubReport Step by step

 再次感謝Jimmy.Yang的分享,此篇博文完全仿照他的博客,只是轉移到了WinForm,因為自己在用的時候走了一些彎路,希望記下來幫助記憶。

 

WinForm RDLC SubReport Step by step

上一篇:【Objective-C】Http常用API、同步请求与异步请求[转]


下一篇:delphi 中 unicode 转汉字 函数