1.沿用之前报表数据源,新建主报表与子报表(以下分别为主,子报表)
2.在子报表中设置钻取参数,即上图中的Department。
3.设置主报表钻取链接。
在文本框属性“操作”一栏。启用为超链接中选择“转到报表”,选择子报表。
之后添加参数,即选择子报表的钻取参数,在值中选择传递的字段。
4.修改子报表数据源(若只移植到web项目中,此步骤可省略。SSRS中不可忽略)
此时,钻取报表即可预览。但为了移植到web项目中,继续...
5.在web项目中,钻取报表必须实现Drillthrough事件。

/// <summary> /// 子报表数据源 /// </summary> /// <param name="para">钻取参数</param> /// <returns>数据源表</returns> private DataTable GetChildData(string para) { DataTable dt = new DataTable(); string text = "select * from HumanResources.vEmployeeDepartment where Department = '" + para + "'"; dt = SqlHelper.ExecuteDataset(BaseCommon.ConnectionString, CommandType.Text, text).Tables[0]; return dt; } protected void rptvContainer_Drillthrough(object sender, DrillthroughEventArgs e) { LocalReport Report = (LocalReport)e.Report; Report.ReportPath = Report.ReportPath.Remove(Report.ReportPath.Length - 1); //Report.DataSources.Add(new ReportDataSource("DataSet1", // GetChildData(Report.OriginalParametersToDrillthrough[0].Values[0]))); ReportParameterInfoCollection co = Report.GetParameters(); if (co.Count > 0) { for (int i = 0; i < co.Count; i++) { if (!string.IsNullOrEmpty(co[i].Values[0])) { Report.DataSources.Add(new ReportDataSource("DataSet1", GetChildData(co[i].Values[0]))); } } } Report.Refresh(); }
注意:若仅适用钻取参数,可使用OriginalParametersToDrillthrough索引器。msdn推荐GetParameters方法。此方法获取报表所有参数集合。
因为web项目默认获取的报表对象为rdlc,但该报表实为rdl,所以截取最后一位,才可与实际路径相符。
以下分别为主、子报表效果图。