4.3 子报表控件使用介绍
子报表作为主报表的一个补充,在显示明细内容的时候比较管用,本文从如何填充子报表数据,如何传递参数两方面介绍子报表如何使用。
1) 新建RDLC报表,命名为subReportDemo.rdlc;
2) 在subReportDemo.rdlc设计器中,拖入一个文本框和一个子报表控件;
3) 设置文本框的文本显示为“子报表控件演示”;
4) 设置子报表控件属性,主要设置其“子报表”的值,这里设为上文讲到的ctlTableDemo报表,效果如下图所示;
5) 新建一个窗体FrmSubReportDemo,添加如下代码
private void FrmCtlSubReportDemo_Load(object sender, EventArgs e)
{
reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
//
this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
this.reportViewer1.LocalReport.ReportPath = @"rdlc/subReportDemo.rdlc";
//
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("ctlsDemo_vSales", LoadData()));
//
this.reportViewer1.RefreshReport();
}
void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources.Add(new ReportDataSource("ctlsDemo_vSales", LoadData()));
}
private DataTable LoadData()
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"data/ctlsDemo.xml");
return dataSet.Tables[0];
}
这里,区别于其它显示RDLC报表程序的代码就是对子报表的处理,本文在FrmCtlSubReportDemo_Load事件里添加了一个对子报表事件的处理:reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
6) 关于子报表参数如何传递,在RDLC报表参数介绍一文中介绍;
7) 运行效果如下图所示
因为不知道如何在博文中上传附件,相关资料已经上传到CSDN,大家可以搜索“RDLC使用手册”下载。