子报表处理事件未触发

时间:2015-09-04 15:26:45

标签: c# reporting-services subreport

尝试调试

时子程序处理事件处理程序未触发
    SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
    dataAdapter.Fill(dataset);
    this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\sale_dept.rdl";
 this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
this.reportViewer1.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(addsubreport);
this.reportViewer1.RefreshReport();




 void addsubreport(object sender, SubreportProcessingEventArgs e)
        {

            SqlConnection conn = new SqlConnection(source);
            DataSet dataset = new DataSet();
            conn.Open();

           SqlCommand sqlcomm = new SqlCommand("subreportquery", conn);

           SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
           dataAdapter.Fill(dataset);

           e.DataSources.Add(new ReportDataSource("discount", dataset.Tables[0]));
        }

当我在调试模式下运行pgm时,上面的处理程序没有触发。

1 个答案:

答案 0 :(得分:1)

我发现了这个问题。我发布它作为答案,因为它可能在将来帮助某人。

SubreportProcessingEventHandler只会被触发.rdlc子报告。在我的项目主报告中,所有子报告都是.rdl扩展名。因此,只有我完成的更改才进入命令提示符并将子报表扩展名重命名为.rdlc

例如: - ren discount.rdl discount.rdlc

然后相应地附加子报告的数据集。

请参阅以下代码

SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlcomm);
dataAdapter.Fill(dataset);
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\main_rpt.rdl";
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(addsubreport);
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dataset.Tables[0]));
this.reportViewer1.RefreshReport();


void addsubreport(object sender, SubreportProcessingEventArgs e)
{

SqlCommand sqlcomm = new SqlCommand();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet dataset = new DataSet();

Switch(e.ReportPath)
{
case "subreport1":
   sqlcomm = new SqlCommand( "Query for subreport one", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport1", dataset.Tables[0]));
   break;
case "subreport2":
   sqlcomm = new SqlCommand( "Query for subreport two", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport2", dataset.Tables[0]));
   break;
case "subreport3":
   sqlcomm = new SqlCommand( "Query for subreport three", conn);
   dataAdapter = new SqlDataAdapter(sqlcomm);
   dataAdapter.Fill(dataset);
   e.DataSources.Add(new ReportDataSource("DataSet for subreport3", dataset.Tables[0]));
   break;

 }

}

如果您有多个子报告,则需要切换。需要注意的要点

1.所有子报告都应该有扩展名.rdlc

2.如果任何参数正在传递,请确保它也被正确命名。

  1. 正确指定路径。将主报表和子报表放在同一文件夹中会更好。
  2. 现在运行C#应用程序,它将显示包含所有子报告的主报告