DevExpress如何创建主从报表
1、新建XtraReport
- 右键Insert Detail Report
- 结构如下图
2、创建数据源
private DataSet GetData()
{
DataSet ds = new DataSet();
string sqlMaster = $"select t.presc_date,t.presc_no, t.name,t.sex ,t.charge_type,t.costs from drug_presc_master t where t.presc_date between to_date('2019-08-17','yyyy-MM-dd') and to_date('2019-08-18','yyyy-MM-dd')";
DataTable dtMaster = DbHelperOracle.Query(sqlMaster).Tables[0];
dtMaster.TableName = "Master";
string sqlDetal = $"select t.presc_date,t.presc_no, t.drug_name,t.drug_spec,t.firm_id,t.administration,t.item_no ,t.quantity,t.costs from drug_presc_detail t where t.presc_date between to_date('2019-08-17','yyyy-MM-dd') and to_date('2019-08-18','yyyy-MM-dd')";
DataTable dtDetail = DbHelperOracle.Query(sqlDetal).Tables[0];
dtMaster.TableName = "Master";
dtDetail.TableName = "Detail";
ds.Tables.Add(dtMaster.Copy());
ds.Tables.Add(dtDetail.Copy());
// 给数据集建立主从关系
DataColumn masterColumnDate = ds.Tables["Master"].Columns["presc_date"];
DataColumn masterColumnNo = ds.Tables["Master"].Columns["presc_no"];
DataColumn detailColumnDate = ds.Tables["Detail"].Columns["presc_date"];
DataColumn detailColumnNo = ds.Tables["Detail"].Columns["presc_no"];
DataColumn[] dcMaster = new DataColumn[] { masterColumnDate, masterColumnNo };
DataColumn[] dcDetail = new DataColumn[] { detailColumnDate, detailColumnNo };
DataRelation relDate = new DataRelation("relationCol", dcMaster, dcDetail);
ds.Relations.Add(relDate);
return ds;
}
3、给报表设置数据源
public XtraReport1()
{
InitializeComponent();
DataSet ds = GetData();
// 绑定主表
this.DataSource = ds;
this.DataMember = "Master";
// 制定从表成员
DetailReport.DataMember = "relationCol";
// 绑定从表
DetailReport.DataSource = ds;
}
4、打印预览
private void BtnPrint_Click(object sender, EventArgs e)
{
// 打印预览
ReportPrintTool tool = new ReportPrintTool(new XtraReport1());
tool.ShowPreviewDialog();
}
世界上没有什么事情是跑步解决不了的,如果有,那就再跑一会!