doit

Report报表学习系列四

因为过春节的缘故,第四篇多等了一年。

上一篇写到表参数设置和建立子表,分组,本篇将学习使用钻取报表。其实只要会使用参数和建立子报表,钻取报表是很简单的事情了。

首先简单地说一下这里的钻取报表的目的:当点击相应的学生后面的查看按钮后,能跳转到另一张报表,显示该学生所在班级的所有学生信息。

一 在文件夹Report中添加报表文件StudentDetail.rdlc,拖放一张表。报表-选择数据源为RptDataSet_Student,报表-建立参数ClassID,选中表,右键-属性-筛选器,设置(=Fields!ClassID.Value)=(=Fields!Sname.Value),确定。

二 在rptStudent.rdlc的表的最右侧插入列,列标题为"查看所在班级",详细内容为"查看",如下图1.1

 
图1.1
右击“查看”所有在的单元格,右键选择属性-导航-跳至表设为rptStudentDetail,然后点参数,设置参数名为刚才上面在StudentDetail里面的ClassID,值为=Fields!ClassID.Value,颜色设为红色.
三 为报表添加钻取报表处理事件。
  private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            e.DataSources .Add (
                new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
                );
        }
四 把委派事件添加到本地报表
在窗体的LOAD事件里面添加如下代码
  this.rptViewMain.Drillthrough += new DrillthroughEventHandler(LocalReport_DrillthroughEventHandler);
整个代码如下:

public FrmRptMain()
        {
            InitializeComponent();
        }

        private ClassDataSource dataSource = new ClassDataSource();
        private StudentDataSource studentDataSource = new StudentDataSource();

        private void FrmRptMain_Load(object sender, EventArgs e)
        {
            this.rptViewMain.LocalReport.SubreportProcessing +=
                 new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

            this.rptViewMain.Drillthrough += new DrillthroughEventHandler(LocalReport_DrillthroughEventHandler);

            this.rptViewMain.LocalReport.ReportEmbeddedResource = "ReportingAPP.Report.rptClass.rdlc";
            this.rptViewMain.LocalReport.DataSources.Add(
                new ReportDataSource("RptDataSet_Class", dataSource.GetClassData())
                );

            this.rptViewMain.RefreshReport();
        }

        private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            e.DataSources.Add(
                new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
                );
        }

        private void LocalReport_DrillthroughEventHandler(object sender, DrillthroughEventArgs e)
        {
            LocalReport rpt = e.Report as LocalReport;
            if (rpt != null)
            {
                rpt.DataSources.Add(new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData()));
            }
        }

五 调试运行

主界面如下:

当点击查看后,结果如下图
六 总结
钻取报表的处理过程和子报表差不多,这里只说最基础的入门知识,更深的东西希望读者自己摸索,这里简单的总结一下过程。
1)建立需要跳转的目标报表,选择相应的数据源和建立相应的参数,设置筛选条件。
2)在主报表中设置链接的字段,设置链接的路径和参数。
3)编写本地报表的钻取事件(为子目标报表提供数据源)
4)添加本地报表的钻取事件。
过完年刚刚回来,第一天上班,还没什么状态,先到这里,Happy new year!

posted on 2011-02-10 21:37  doit  阅读(676)  评论(1编辑  收藏  举报