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#

第一步,新建项目

2.在项目中新建数据集

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

4.在项目中新建报表rptDEPT

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

6. 在Form1里新建一个ReportViewer1.

并填上如下代码

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;

        }

    }

}

  

然后运行结果显示如下:

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

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

在子报表中增加表和字段

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

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

 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 }

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

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

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

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

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

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

最终Form1.cs代码:

  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 }

运行结果如下:

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

时间: 2024-08-06 01:48:31

WinForm RDLC SubReport Step by step的相关文章

WPF Step By Step 控件介绍

WPF Step By Step 控件介绍 回顾 上一篇,我们主要讨论了WPF的几个重点的基本知识的介绍,本篇,我们将会简单的介绍几个基本控件的简单用法,本文会举几个项目中的具体的例子,结合这些 例子,希望我们可以对WPF的掌握会更深刻.本文涉及的内容可能较多.请大家慢慢看看.错误之处,还请指出. 本文大纲 1.基本控件介绍与用法. 基本控件介绍与用法 文本控件 Label控件 label控件:一般用户描述性文字显示. 在Label控件使用时,一般给予用户提示.用法上没有什么很特殊的,label

数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSGS算法中是要求a^m在%c条件下的逆元的,如果a.c不互质根本就没有逆元.) 如果x有解,那么0<=x<C,为什么? 我们可以回忆一下欧拉定理: 对于c是素数的情况,φ(c)=c-1 那么既然我们知道a^0=1,a^φ(c)=1(在%c的条件下).那么0~φ(c)必定是一个循环节(不一定是最小的)

Git Step by Step – (8) Git的merge和rebase

前面一篇文章中提到了"git pull"等价于"git fetch"加上"git merge",然后还提到了pull命令支持rebase模式,这篇文章就介绍一下merge和rebase之间有什么差别. 由于我们主要是想看看merge跟rebase之间的区别,这里就是用本地仓库的分支进行演示了. merge 其实在介绍分支的那篇文章中已经介绍过了一些分支merge的内容,这里就进行一些补充和总结. 下面我们基于本地一个仓库开始介绍,当前仓库的分支情

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures things about 1. Declare an enumeration type. 2. Create and use an enumeration type. 3. Declare a structure type. 4. Create and use a structure type. 5. Explain

Linux Booting Process: A step by step tutorial for understanding Linux boot sequence

One of the most remarkable achievement in the history of mankind is computers. Another amazing fact about this remarkable achievement called computers is that its a collection of different electronic components, and they work together in coordination

C++开发WPF,Step by Step

示例代码 使用C++来开发WPF,主要是如何在MFC(Win32)的窗口中Host WPF的Page.下面我就做个详细的介绍. 一.创建工程, 由于MFC的Wizard会生成很多用不到的代码,所以我准备从一个空的工程开始创建一个MFC的工程. a)         打开VS2005,菜单File->New->Projects-, 左面选择Visual C++->Win32,右面选择Win32 Console Application,给工程起个名字CPlusPlus_WPF, Ok进入下一

数据库设计 Step by Step (1)——扬帆启航

引言:一直在从事数据库开发和设计工作,也看了一些书籍,算是略有心得.很久之前就想针 对关系数据库设计进行整理.总结,但因为种种原因迟迟没有动手,主要还是惰性使然.今天也算是痛下决心开始这项卓绝又令我兴奋的工作.这将是一个系列的文 章,我将以讲座式的口吻展开讨论(个人偷懒,这里的总结直接拿去公司培训新人用). 系列的第一讲我们先来回答下面几个问题 数据库是大楼的根基 大多数程序员都很急切,在了解基本需求之后希望很快的进入到编码阶段(可能只有产出代码才能反映工作量),对于数据库设计思考得比较少. 这

数据库设计 Step by Step (2)——数据库生命周期

引言:数据库设计 Step by Step (1)得到这么多朋友的关注着实出乎了我的意外.这也坚定了我把这一系列的博文写好的决心.近来工作上的事务比较繁重,加之我期望这个系列的文章能尽可能的系统.完整,需要花很多时间整理.思考数据库设计的各种资料,所以文章的更新速度可能会慢一些,也希望大家能够谅解. 系列的第二讲我们将站在高处俯瞰一下数据库的生命周期,了解数据库设计的整体流程 数据库生命周期 大家对软件生命周期较为熟悉,数据库也有其生命周期,如下图所示. 图(1)数据库生命周期 数据库的生命周期

Shell Step by Step (3) —— Stdin &amp;amp; if

4.输入输出 #! /bin/bash # Read users input and then get his name read -p "Please input your first name: " firstName read -p "Please input your last name: " lastName echo -e "Your full name is: $firstName $lastName" read使用方法: read