如果一份报表是用来显示分层数据源的数据,则它通常被称为主从报表。本文介绍了在DevExpress XtraReports控件基于DetailReport通过代码绑定主从数据源生成主从报表。以下是报表效果图。
前端设计
前端插入2个嵌套的DetailReport控件。
后端设计
1.获取数据源,并设置两个表主从关系。
private DataSet GetDataSource()
{
DataSet ds = new DataSet();
string ConStr = SH.Business.Utilities.GetHsLink(ManageProvider.Provider.Current().CompanyId);
//查询主从2个数据源
using (MiniDapper.Database db = new MiniDapper.Database(ConStr, "SqlProvider"))
{
string ParentSql = string.Format(@"select A.Id,A.sOrderNo,A.sMaterialLot,A.sMaterialName,A.UserName,A.sCreateTime
from ParentDt A where A.uGuid='{0}'", uGuid);
var ParentDt = db.GetDataTable(ParentSql);
ParentDt.TableName = "ParentDt";
ds.Tables.Add(ParentDt);
string ChildSql = string.Format(@"select A.ParentId,A.sCardNo,A.sOrderNo,A.sCustomerName,A.sMaterialLot,A.sMaterialName,A.sColorNo,A.sColorName,A.UserName,A.sCreateTime
from ChildDt A where A.uGuid='{0}'", uGuid);
var ChildDt = db.GetDataTable(ChildSql);
ChildDt.TableName = "ChildDt";
ds.Tables.Add(ChildDt);
}
//给数据集建立主外键关系(主从表)
DataColumn ParentColumn = ds.Tables["ParentDt"].Columns["Id"];
DataColumn ChildColumn = ds.Tables["ChildDt"].Columns["ParentId"];
DataRelation Rel = new DataRelation("RelationColumn", ParentColumn, ChildColumn);
ds.Relations.Add(Rel);
return ds;
}
2.绑定数据到报表文件
public void DataSourceBindings(DataSet ds)
{
//绑定主表
DetailReport.DataSource = ds;
DetailReport.DataMember = "ParentDt";
lb_sOrderNo.DataBindings.Add("Text", ds, "ParentDt.sOrderNo");
lb_sMaterialLot.DataBindings.Add("Text", ds, "ParentDt.sMaterialLot");
lb_sMaterialName.DataBindings.Add("Text", ds, "ParentDt.sMaterialName");
DetailReport1.DataMember = "RelationColumn";
//绑定从表
DetailReport1.DataSource = ds;
this.xc_sCardNo.DataBindings.Add("Text", ds, "RelationColumn.sCardNo");
this.xc_sColorNo.DataBindings.Add("Text", ds, "RelationColumn.sColorNo");
this.xc_sColorName.DataBindings.Add("Text", ds, "RelationColumn.sColorName");
}