.net 流氓
天下无难事,有志者成之;天下无易事,有恒者得之。

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

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

一 在文件夹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)添加本地报表的钻取事件。
posted on 2011-08-09 10:37  .net 流氓  阅读(428)  评论(0编辑  收藏  举报