RDLC 报表系列(四) 子报表的使用

本文只代表作者在一定阶段的认识与理解

一.写作前提

前面几篇中我讲了如何去实现一个RDLC报表、插入图片、参数传递及常用表达式的使用。这一节我们将focus on子报表的使用。假设有一天,你的老板和你说,公司需要做所有员工的出出差记录,这时子报表的应用是一个不错的选择。

         关于怎样创建一个RDLC报表等相关内容,前查看我之前的文章,这里就不在阐述了。

[原创] RDLC 报表系列(一) 创建一个报表       

[原创] RDLC 报表系列(二) 报表中插入图片

[原创] RDLC 报表系列(三) 参数、常量及常用表达式的使用

二. 本文内容

1. RDLC报表模板的设计

2.  Base的修改

3.  调用生成报表 

4.  总结

5.  代码下载(下载)

三.RDLC报表模板设计

      本篇要做的是一个含有子报表的报表,所以,最少我们需要两个报表模板,一个是Master.rdlc,另外一个是Sub.rdlc。Ok,下面我们就来啰嗦一下怎么设计模板文件吧。

1.       在VS(本人用的是2008的Version,自从小2003之后都支持.rdlc报表)中创建一个新的web application项目。

2.       新增两个新的报表文件,一个名叫master.rdlc,一个名叫sub.rdlc(在选择的时候要注意选择如下)。

      

3.       现在我们已经有报表模板文件了,再创建主报表和子报表所需要使用的数据类型(所需要的DataSet类型,具体可参阅[原创] RDLC 报表系列(一) 创建一个报表),这里并不多做解释了。

4.        打开 style="mso-bidi-font-weight:bold">master.rdlc文件,从工具箱中拖入一表格控件,设计你所需要的格式,选择所需要的数据源类型,然后在你需要使用子报表的地方从工具臬中插入一上SubReport控件,如下图所示。

       

5.       然后右击这个SubReport控件,在属性中选择这个子报表指向哪个文件,我们可以选择刚才创建的那个sub.rdlc文件。其次是们还需要为主报表和子报表之关建立关连关系,在属性的Parameters(参数)选择卡中新加我们需要传递给子报表的参数。

在我的报表中用了一个参数如下,当然你也可以根据自己的的需要创建多个你需要传递给子报表的参数。

     

6.       上面我们讲解完了主报表的设计,他制定了子报表以及子报表准获得的参数。现在打开Sub.rdlc文件,增加子报表所需要的数据源类型,并且创建参数,比如说我上面使用了一个isoOid,所以在子报表中一定有一个参数名称和这个是相同的,他用来接收从主报表传来的筛选数据条件,即用这个参数从子报表中选择符合本记录的所有子记录集。

7.       现在从工具箱中插入一个表格到你的子报表中(当然也可以不需要表格,根据需求定义的),然后右击属性选择所需要的数据源类型,并且根据从Master.rdlc传过来的参数进行筛选,可以在属性窗口的Filters(筛选)选项卡中加入筛选条件即可。

8.       至此我们讲完了报表设计部分,下面我们就去看看,C#中是如何实现的。

 

四.Base的修改

     因为是要用到报表,所以我们需要对子报表的生成事件进行订阅,因此我们把CustomPageBase写成如下。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 public class CustomPageBase : Page
    {
        List<ReportDataSource> subDataSource = new List<ReportDataSource>();
        public void GetReportMultipleDataSourceFile(List<ReportDataSource> reportDateSource, string TemplatePath, List<ReportParameter> parameterList, string FileType, List<string> subDataList)
        {
            string reportFormat = FileType;
            string outputfile = "Report.";  //报表名称
             ReportViewer rview = new ReportViewer();
            rview.ProcessingMode = ProcessingMode.Local;
            rview.LocalReport.ReportPath = Server.MapPath(TemplatePath);
            rview.LocalReport.DataSources.Clear();
            
            //为主报表加数据源
             foreach (ReportDataSource re in reportDateSource)
            {
                if (subDataList.Contains(re.Name))
                {
                    subDataSource.Add(re);
                    continue;
                }
                rview.LocalReport.DataSources.Add(re);
            }

            //设置ReportViewer进行事件订阅
             rview.LocalReport.SubreportProcessing +=
                  new SubreportProcessingEventHandler(SubreportProcessingEventHandler);

            if (parameterList.Count > 0)
                rview.LocalReport.SetParameters(parameterList);
            string mimeType, encoding, extension, deviceInfo;
            string[] streamids;
            Warning[] warnings;
            deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>";

            byte[] bytes = rview.LocalReport.Render(reportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = mimeType;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + outputfile + extension + ";");
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 为子报表加数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
        {
            foreach (var ReportDataSource in subDataSource)
            {
                e.DataSources.Add(ReportDataSource);
            }
        }
    }

和这前的相关,GetReportMultipleDataSourceFile这个文件多提供一了一个新的参数subDataList他是用来指定哪些数据源是给子报表的

五.调用生成报表

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 public partial class _Default : CustomPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Generate the report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         protected void ButtonGenerate_Click(object sender, EventArgs e)
        {
            List<ReportDataSource> reportDataSource = new List<ReportDataSource>();
            List<string> subDataSource = new List<string>();
            ReportDataSet ds = new ReportDataSet();
            string templatePath = string.Empty;

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PQMSConnectionString"].ConnectionString);
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "select top 200 Oid, DrawingNumber, CWPNo, PaintCode, MasterRevision from ISO_DRAWING";
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(ds, "ISO_DRAWING");  //获得主表的内容(Get master table data)
 
            comm.CommandText = "SELECT S.Oid, S.ISOOid, S.SPOOLNo, S.Location, S.IssueNo, S.CP2 FROM SPOOL AS S INNER JOIN (SELECT TOP 200 OID FROM ISO_DRAWING) AS T ON S.ISOOID = T.OID";
            da = new SqlDataAdapter(comm);
            da.Fill(ds, "SPOOL");

            reportDataSource.Add(new ReportDataSource("ReportDataSet_ISO_DRAWING", ds.ISO_DRAWING));
            reportDataSource.Add(new ReportDataSource("ReportDataSet_SPOOL", ds.SPOOL));
            subDataSource.Add("ReportDataSet_SPOOL");
            List<ReportParameter> parameterList = new List<ReportParameter>();

            templatePath = "Master.rdlc";

            GetReportMultipleDataSourceFile(reportDataSource, templatePath, parameterList, "PDF", subDataSource);
        }
    }

六.总结

    通过上面的学习,我们了解如下内容:

1. Sub Report Design

2.  Filter的应用


from:http://www.cnblogs.com/jack86514/archive/2010/01/22/1653762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值