rdlc报表在子报表中显示数据代码如下:
ReportViewerService.ReportViewerService ds = new ReportViewerService.ReportViewerService();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["PrintDataSource"] != null)
{
repStuInfoData rsd = new repStuInfoData();
rsd.dtStuInfo.Merge(LoadData());
if (rsd.dtStuInfo.Rows.Count > 0)
{
//设置数据源
rvStuInfo.ProcessingMode = ProcessingMode.Local;
//设置报表
rvStuInfo.LocalReport.ReportPath = "WebPage/Reports/repStuInfo.rdlc";
rvStuInfo.LocalReport.DataSources.Clear();
rvStuInfo.LocalReport.DataSources.Add(new ReportDataSource("repStuInfoData_dtStuInfo", rsd.dtStuInfo));
rvStuInfo.LocalReport.Refresh();
rvStuInfo.LocalReport.SubreportProcessing += new
SubreportProcessingEventHandler(DemoSubreportProcessingEventHandler);
}
}
}
void DemoSubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
IList<string> str_student = e.Parameters[0].Values;
DataTable picdt = null;
try
{
picdt = ds.ReportImages(str_student[0].ToString());
}
catch
{
picdt = new DataTable("repStuInfoData_work_photo");
DataColumn column;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Byte[]");
column.ColumnName = "photo";
picdt.Columns.Add(column);
}
e.DataSources.Add(new ReportDataSource("repStuInfoData_work_photo", picdt));
}
private DataTable LoadData()
{
return JSONHelper.JsonToDataTable(Session["PrintDataSource"].ToString());
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["PrintDataSource"] != null)
{
repStuInfoData rsd = new repStuInfoData();
rsd.dtStuInfo.Merge(LoadData());
if (rsd.dtStuInfo.Rows.Count > 0)
{
//设置数据源
rvStuInfo.ProcessingMode = ProcessingMode.Local;
//设置报表
rvStuInfo.LocalReport.ReportPath = "WebPage/Reports/repStuInfo.rdlc";
rvStuInfo.LocalReport.DataSources.Clear();
rvStuInfo.LocalReport.DataSources.Add(new ReportDataSource("repStuInfoData_dtStuInfo", rsd.dtStuInfo));
rvStuInfo.LocalReport.Refresh();
rvStuInfo.LocalReport.SubreportProcessing += new
SubreportProcessingEventHandler(DemoSubreportProcessingEventHandler);
}
}
}
void DemoSubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
IList<string> str_student = e.Parameters[0].Values;
DataTable picdt = null;
try
{
picdt = ds.ReportImages(str_student[0].ToString());
}
catch
{
picdt = new DataTable("repStuInfoData_work_photo");
DataColumn column;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Byte[]");
column.ColumnName = "photo";
picdt.Columns.Add(column);
}
e.DataSources.Add(new ReportDataSource("repStuInfoData_work_photo", picdt));
}
private DataTable LoadData()
{
return JSONHelper.JsonToDataTable(Session["PrintDataSource"].ToString());
}
其中有几个关键点要特别注意:
1.主报表和子报表的参数一定要设置一样。
2.子报表的数据源必须在代码中通过设置SubreportProcessing 事件来添加。
3.主报表和子报表的路径一定要设置正确。
4.非常重要的一点,也是容易出错的一点,主报表和子报表的dataset name一定要设置正确,这个name值可以通过右键点击报表文件,在打开方式中选【xml编辑器】打开,看
DataSet Name的值是什么就填什么,这个如果设置错了就无法读取到主报表的数据,或子报表无法显示。