2016-07-08
//打印主从表数据
string file = Application.StartupPath @"\MasterDetail.frx";
rptMasterDetail.Load(file);//加载报表模板文件
DataSet ds = DAL.GetMasterDetailData();//取报表数据
rptMasterDetail.RegisterData(ds.Tables[0], "tb_SO"); //注册数据源,主表
rptMasterDetail.RegisterData(ds.Tables[1], "tb_SOs"); //注册数据源,从表
//
//直接注册DataSet也行,但必须对DataSet.Tables指定表名!
//FastReport是跟据表名取DataTable对象的。
//
//rptMasterDetail.RegisterData(ds);
//
//给DataBand(主表数据)绑定数据源
DataBand masterBand = rptMasterDetail.FindObject("Data1") as DataBand;
masterBand.DataSource = rptMasterDetail.GetDataSource("tb_SO"); //主表
//给DataBand(明细数据)绑定数据源
DataBand detailBand = rptMasterDetail.FindObject("Data2") as DataBand;
detailBand.DataSource = rptMasterDetail.GetDataSource("tb_SOs"); //明细表
//重要!!给明细表设置主外键关系!
detailBand.Relation = new Relation();
detailBand.Relation.ParentColumns = new string[] { "SONO" };
detailBand.Relation.ParentDataSource = rptMasterDetail.GetDataSource("tb_SO"); //主表
detailBand.Relation.ChildColumns = new string[] { "SONO" };
detailBand.Relation.ChildDataSource = rptMasterDetail.GetDataSource("tb_SOs"); //明细表
//准备工作,显示报表预览窗体
rptMasterDetail.Prepare();
rptMasterDetail.ShowPrepared(true, this);