RDLC 子报表运行模式

来源:互联网 发布:儿歌知错的小鸭 编辑:程序博客网 时间:2024/05/17 09:11

在开发RDLC子报表时,大致上的程式码会是如下

// 显示Report的Method private  void Show_Report(){    ....    ....        // 指定子报表事件处理常式    this .ReportViewer1.LocalReport.SubreportProcessing +=  new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);    // Run Query SQL存入datatable,然后指定给RDLC报表档(程式码略…..)     ….    ….    this .ReportViewer1.LocalReport.DataSources.Add( new Microsoft.Reporting.WebForms.ReportDataSource( " XXXXX " , dt));     this .ReportViewer1.LocalReport.Refresh();}void LocalReport_SubreportProcessing( object sender, SubreportProcessingEventArgs e){    // Exec子报用的Query SQL存入datatable,然后指定给RDLC报表档(程式码略…..)     ......    ......        e.DataSources.Add( new Microsoft.Reporting.WebForms.ReportDataSource( " XXXXX " , dt);}

然而值得注意的是,这样的方式在LocalReport_SubreportProcessing里是每一页主报表的资料都会进去Query一次子报用的Query SQL,所以当你把报表设计成不分页的情况下,会变成像是在一个Loop里不断的Query SQL,所以报表资料愈多,Query愈多次,效能肯定不会太好,因此较好的写法可以改成如下的方式,原理就是减少重覆执行Query的动作

private  void Show_Report(){  // 一次Query主报表及子报表要用的资料,放入所属的DataTable   // master_dt (程式码略...)   // sub_dt (程式码略...)    // 指定子报表事件处理常式   this .ReportViewer1.LocalReport.SubreportProcessing +=  new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);    this .ReportViewer1.LocalReport.DataSources.Add( new Microsoft.Reporting.WebForms.ReportDataSource( " xxx " , master_dt));     this .ReportViewer1.LocalReport.Refresh();}void LocalReport_SubreportProcessing( object sender, SubreportProcessingEventArgs e){    // 利用Sub DataTable的资料,直接指定给子报用就好,不再重覆Query     e.DataSources.Add( new Microsoft.Reporting.WebForms.ReportDataSource( " xxxxx " , sub_dt);}



原创粉丝点击