创建RDLC子报表

一、创建步骤​

1、建立主报表​

2、建立子报表​,添加参数,用于接受主报表关联字段的值。

3、在主报表中插入"子报表"控件,设置子报表的"常规"和"参数"属性。"常规"属性用于设定子报表的名称,"参数"属性用于将子报表与主报表通过子报表的参数、主报表的关联字段将二者关联起来。​

4、在后台代码中为主报表指定ReportDataSource​

5、在后台代码中注册(也称为订阅)子报表事件​

6、在子报表事件中为子报表指定ReportDataSource​

二、创建示例​

1、建立主报表​​

2、建立子报表​,并添加参数ygbm

3、在主报表中插入"子报表"控件,设置子报表的"常规"和"参数"属性。

3.1插入子报表​

3.2设置子报表属性-常规​

3.3设置子报表属性-参数​​,名称为子报表中定义的参数名称,值为主报表关联子报表的字段

4、在后台代码中为主报表指定ReportDataSource​​

protected void Button1_Click(object sender, EventArgs e)

{

string ygbm = TextBox1.Text ;

DataAccess myda = new DataAccess();

string selStr = "select ygbm,ygxm,(SELECT CASE WHEN ygxb = '1' THEN '男' ELSE '女' END) as ygxb,";

selStr+="ygmz,sfid,zzmm,ygsr,gzsj,byxx,xlmc,zcmc,jnmc,zwmc,lxdh,gzgw,memo from vw_ygb where ygbm=" + ygbm;

DataSet ds = myda.GetDataSet(selStr);

ReportDataSource rds = new ReportDataSource("DataSet1", ds.Tables[0]);

ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

ReportViewer1.Visible = true;

ReportViewer1.LocalReport.DataSources.Clear();

ReportViewer1.LocalReport.DataSources.Add(rds);

ReportViewer1.LocalReport.Refresh();

}

5、在后台代码中注册(也称为订阅)子报表事件​​

ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

6、在子报表事件中为子报表指定ReportDataSource​​

void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)

{

int ygbm = int.Parse(e.Parameters["ygbm"].Values[0]);

DataAccess myda = new DataAccess();

string selStr = "select * from vw_zyb where ygbm="+ygbm;

DataSet ds = myda.GetDataSet(selStr);

ReportDataSource rds = new ReportDataSource("DataSet1", ds.Tables[0]);

e.DataSources.Add(rds);

}​

7、运行结果如下​

 

posted @ 2015-09-23 15:13  xashxsy  阅读(937)  评论(0编辑  收藏  举报