Rdlc子报表的动态添加

大概注意事项:

1.确定主报表中子报表的名称与动态加载报表的名称一样
2.子报表加载数据的事件在加载子报表流的后面
3.报表要重新Reset并处理过Refresh
4.eg:
      void BindReport()
      {
             //动态create报表
             CreatesubRdlc();

            ReportViewer1.Reset();
            StreamReader mainstream = new StreamReader(Server.MapPath("~/RdlcDoc/" + Request.QueryString["ReportName"].ToString() + ".rdlc"));
            ReportViewer1.LocalReport.LoadReportDefinition(mainstream );
            mainstream .Close();
            StreamReader substream = new StreamReader(Server.MapPath("~/RdlcDoc/" + prerpt + strMenuId + "CommRdlc.rdlc"));
            ReportViewer1.LocalReport.LoadSubreportDefinition("CommRdlc", substream);
            substream.Close();

            //加子报表事件
            ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);

            //开发主报表
            ReportViewer1.LocalReport.DataSources.Clear();
           //加载主表数据
            DataTable dt = ((saveSession)Session["pintQryData"]).dtdata;
            ReportDataSource rds1 = new ReportDataSource("ReportDB", dt);
            ReportViewer1.LocalReport.DataSources.Add(rds1);
            //加参数
            ReportParameter[] inpara = ((saveSession)Session["pintQryData"]).rptParam;
            ReportViewer1.LocalReport.SetParameters(inpara);
            //refresh
            ReportViewer1.LocalReport.Refresh();
           //删除临时产生报表
            System.IO.File.Delete(Server.MapPath(prerpt + strMenuId + "CommRdlc.rdlc"));
    }
    /// <summary>
    /// 为子报表加数据源
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
  void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
      {
          DataTable dt = ((saveSession)Session["pintQryData"]).dtdata;
          ReportDataSource rds1 = new ReportDataSource("ReportDB", dt);
          e.DataSources.Add(rds1);
      }

 

你可能感兴趣的:(报表)