博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RDLC 主从报表筛选
阅读量:4670 次
发布时间:2019-06-09

本文共 4166 字,大约阅读时间需要 13 分钟。

今天继续学习RDLC报表的“参数传递”及“主从报表”

一、先创建DataSet,如下图:

二、创建一个报表rptDEPT.rdlc,显示部门T_DPET的数据

三、嵌入Default.aspx中,写在Default.aspx.cs中写些基本代码

 

using System;using System.Data;using Microsoft.Reporting.WebForms; namespace ReportSample{    public partial class Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                this.ReportViewer1.LocalReport.ReportPath = "rptDEPT.rdlc";                this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DS_DEPT", GetDeptData()));            }        }          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;                        }             }}

 

 

 

 运行效果:

OK,下面才是真正开始:

很多情况下(比如团队开发),报表的数据源DataTable通常是由其它人写好的,有些甚至不允许再做修改,报表开发人员只能被动的接收数据,但是报表上未必需要显示全部数据,以上面的报表为例,如果我们只需要显示"02技术部“的数据,如何处理?

这时报表参数就派上用场了:

四、添加报表参数

在Report Data面板中,选中Parameters,右击-->Add Parameter

为参数取名为DeptNo,并做一些设置,如下图

五、为报表的Table添加Filters条件

上一步添加的参数需要与报表上的Table建立联系,否则发挥不了作用。幸好每个Table都可以设置Filters表达式,来对数据进行筛选,见下图:

六、在cs代码中动态传入参数

修改Default.aspx.cs的代码,在运行时动态添加参数

protected void Page_Load(object sender, EventArgs e){    if (!IsPostBack)    {        this.ReportViewer1.LocalReport.ReportPath = "rptDEPT.rdlc";                       this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DS_DEPT", GetDeptData()));        //动态传入参数        this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("DeptNo", "02"));    }}

 

最终运行结果:

很多报表中,数据的来源往往不止一个DataTable,下面我们模拟一个简单的主从报表,主报表即为上面的rptDEPT(显示部门信息),子报表(也称从报表)显示部门下的员工清单(命名为rptEMP.rdlc)

七、创建员工报表rptEMP.rdlc

布局如下:

同样,我们也为子报表添加一个参数DeptNo,同时还要为子报表的Table设置Filters条件(条件的值在本例中跟主报表相同,同样都是)

八、在rptDEPT.rdlc中插入子报表rptEMP.rdlc

子报表控件允许在一个报表中再插入另一个报表,如下图:

然后在子报表上右击,调出子报表属性

设置加载哪个子报表

同时增加一个子报表参数

注:这里增加一个跟主报表同名的参数DeptNo,同时设置其值为主报表rptDEPT的参数@DeptNo

九、修改Default.aspx.cs代码

 

using System;using System.Data;using Microsoft.Reporting.WebForms; namespace ReportSample{    public partial class Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                //定义子报表处理方法                this.ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);                this.ReportViewer1.LocalReport.ReportPath = "rptDEPT.rdlc";                                  this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DS_DEPT", GetDeptData()));                //动态传入参数                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter("DeptNo", "02"));            }        }         void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)        {            e.DataSources.Add(new ReportDataSource("DS_EMP", GetEMPData()));        }          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;                        }         DataTable GetEMPData()        {            DataTable dt = new DataTable();            dt.Columns.Add("EMPNO", typeof(string));            dt.Columns.Add("EMPNAME", typeof(string));            dt.Columns.Add("DEPTNO", typeof(string));             dt.Rows.Add("001", "杨过","02");            dt.Rows.Add("002", "令狐冲", "02");            dt.Rows.Add("003", "黄蓉", "01");            dt.Rows.Add("004", "小师妹", "03");            dt.Rows.Add("005", "赵敏", "04");             return dt;        }             }}

 

 

 

  最终运行效果:

想想发生了什么?

主报表rptDept与子报表rptEMP设置了相同的参数以及过滤条件,代码给主报表rptDept传递了参数DeptNo后,主报表rptDept又把参数值传递给子报表rptEMP,最终二个报表都实现了数据筛选.

 

 

 

  

 

转载于:https://www.cnblogs.com/daxiongblog/p/9223170.html

你可能感兴趣的文章
css div框加小箭头
查看>>
Eclipse快捷键与使用技巧总结
查看>>
Solr4.8.0源码分析(16)之SolrCloud索引深入(3)
查看>>
PEP8 - Python编码规范
查看>>
div放置图片总结
查看>>
FZOJβ #45. 染色问题
查看>>
Python之SYS模块
查看>>
webapi文件上传和下载
查看>>
HDU 1540 Tunnel Warfare [二分 + 线段树]
查看>>
C++:构造函数和析构函数能否为虚函数
查看>>
win7便笺元数据损坏,最新解决办法
查看>>
mongod
查看>>
vim配置python高亮和缩进
查看>>
Spring3.0.5 获取表中自增的主键(mysql)
查看>>
delphi dxBarManager 的dxBarEdit 输入问题
查看>>
Hadoop入门介绍一
查看>>
面试经典-分金条
查看>>
利用AutoSPSourceBuilder和Autospinstaller自动安装SharePoint Server 2013图解教程——Part 1...
查看>>
ZOJ-2972-Hurdles of 110m(记忆化搜索)
查看>>
一些新了解到技术
查看>>