Create a Master-Detail Report
- 7 minutes to read
This topic demonstrates how to create a master-detail report in code.
To use this code in your application, add references to the assemblies listed in the following help topic: Redistribution and Deployment.
Create a Report and Bind to Data
The code below outlines the order of procedure calls required to create different parts of the master-detail report. The data source is created as described in following help topic: Bind a Report to a Microsoft SQL Server Database at Runtime.
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System.Drawing;
// ...
public static XtraReport CreateReport(object dataSource)
{
SqlDataSource ds = dataSource as SqlDataSource;
if (ds == null) return new XtraReport();
// Create an empty report.
XtraReport report = new XtraReport();
// Bind the report to a data source.
report.DataSource = ds;
report.DataMember = ds.Queries[0].Name;
// Create a master part.
CreateReportHeader(report, "Products by Categories");
CreateDetail(report);
// Create a detail part.
CreateDetailReport(report, ds.Queries[0].Name + "." + ds.Relations[0].Name);
return report;
}
The CreateReportHeader, CreateDetail, and CreateDetailReport method code is listed below.
Create a Master Report Section
The following code creates content for the report’s master section.
The code adds a ReportHeaderBand and DetailBand to the collection of report bands, and adds the report controls to the bands.
The label control is placed on the Report Header to display static text.
Another label is placed on the Detail band. The label’s ExpressionBindings property is specified to retrieve data from the CategoryName
field of the report’s data source.
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System.Drawing;
// ...
private static void CreateReportHeader(XtraReport report, string caption)
{
// Create a report title.
XRLabel label = new XRLabel();
label.Font = new Font("Tahoma", 12, FontStyle.Bold);
label.Text = caption;
label.WidthF = 300F;
// Create a report header and add the title to it.
ReportHeaderBand reportHeader = new ReportHeaderBand();
report.Bands.Add(reportHeader);
reportHeader.Controls.Add(label);
reportHeader.HeightF = label.HeightF;
}
private static void CreateDetail(XtraReport report)
{
// Create a new label bound to the CategoryName data field.
XRLabel labelDetail = new XRLabel();
labelDetail.Font = new Font("Tahoma", 10, FontStyle.Bold);
labelDetail.WidthF = 300F;
// Bind the label to the CategoryName data field.
labelDetail.ExpressionBindings.Add(
new ExpressionBinding("BeforePrint", "Text", "'Category: ' + [CategoryName]"));
// Create a detail band and display the category name in it.
DetailBand detailBand = new DetailBand();
detailBand.Height = labelDetail.Height;
detailBand.KeepTogetherWithDetailReports = true;
report.Bands.Add(detailBand);
labelDetail.TopF = detailBand.LocationFloat.Y + 20F;
detailBand.Controls.Add(labelDetail);
}
Create a Detail Report Section
The code creates new Report Header and Detail bands at the detail level, and adds a table control to each band.
Table cells in the Detail band are bound to the data fields of the report data source. The table uses different styles for odd and even rows.
The width of both tables is set to the effective page width.
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System.Drawing;
// ...
private static void CreateDetailReport(XtraReport report, string dataMember)
{
// Create a detail report band and bind it to data.
DetailReportBand detailReportBand = new DetailReportBand();
report.Bands.Add(detailReportBand);
detailReportBand.DataSource = report.DataSource;
detailReportBand.DataMember = dataMember;
// Add a header to the detail report.
ReportHeaderBand detailReportHeader = new ReportHeaderBand();
detailReportBand.Bands.Add(detailReportHeader);
XRTable tableHeader = new XRTable();
tableHeader.BeginInit();
tableHeader.Rows.Add(new XRTableRow());
tableHeader.Borders = BorderSide.All;
tableHeader.BorderColor = Color.DarkGray;
tableHeader.Font = new Font("Tahoma", 10, FontStyle.Bold);
tableHeader.Padding = 10;
tableHeader.TextAlignment = TextAlignment.MiddleLeft;
XRTableCell cellHeader1 = new XRTableCell();
cellHeader1.Text = "Product Name";
XRTableCell cellHeader2 = new XRTableCell();
cellHeader2.Text = "Unit Price";
cellHeader2.TextAlignment = TextAlignment.MiddleRight;
tableHeader.Rows[0].Cells.AddRange(new XRTableCell[] { cellHeader1, cellHeader2 });
detailReportHeader.Height = tableHeader.Height;
detailReportHeader.Controls.Add(tableHeader);
// Adjust the table width.
tableHeader.BeforePrint += tableHeader_BeforePrint;
tableHeader.EndInit();
// Create a detail band.
XRTable tableDetail = new XRTable();
tableDetail.BeginInit();
tableDetail.Rows.Add(new XRTableRow());
tableDetail.Borders = BorderSide.Left | BorderSide.Right | BorderSide.Bottom;
tableDetail.BorderColor = Color.DarkGray;
tableDetail.Font = new Font("Tahoma", 10);
tableDetail.Padding = 10;
tableDetail.TextAlignment = TextAlignment.MiddleLeft;
XRTableCell cellDetail1 = new XRTableCell();
XRTableCell cellDetail2 = new XRTableCell();
cellDetail2.TextAlignment = TextAlignment.MiddleRight;
cellDetail1.ExpressionBindings.Add(
new ExpressionBinding("BeforePrint", "Text", "[ProductName]"));
cellDetail2.ExpressionBindings.Add(
new ExpressionBinding("BeforePrint", "Text",
"FormatString('{0:$0.00}', [UnitPrice])"));
tableDetail.Rows[0].Cells.AddRange(new XRTableCell[] { cellDetail1, cellDetail2 });
DetailBand detailBand = new DetailBand();
detailBand.Height = tableDetail.Height;
detailReportBand.Bands.Add(detailBand);
detailBand.Controls.Add(tableDetail);
// Adjust the table width.
tableDetail.BeforePrint += tableDetail_BeforePrint;
tableDetail.EndInit();
// Create and assign different odd and even styles.
XRControlStyle oddStyle = new XRControlStyle();
XRControlStyle evenStyle = new XRControlStyle();
oddStyle.BackColor = Color.WhiteSmoke;
oddStyle.StyleUsing.UseBackColor = true;
oddStyle.Name = "OddStyle";
evenStyle.BackColor = Color.White;
evenStyle.StyleUsing.UseBackColor = true;
evenStyle.Name = "EvenStyle";
report.StyleSheet.AddRange(new XRControlStyle[] { oddStyle, evenStyle });
tableDetail.OddStyleName = "OddStyle";
tableDetail.EvenStyleName = "EvenStyle";
}
private static void AdjustTableWidth(XRTable table)
{
XtraReport report = table.RootReport;
table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;
}
static void tableHeader_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
AdjustTableWidth(sender as XRTable);
}
static void tableDetail_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
AdjustTableWidth(sender as XRTable);
}
Sample Projects
The complete code is available in the following examples: