Skip to main content

Create a Master-Detail Report

  • 7 minutes to read

This topic demonstrates how to create a master-detail report in code.

Master-Detail Report Created at Runtime

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");

    // 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();
    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.
        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;
    labelDetail.TopF = detailBand.LocationFloat.Y + 20F;

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();
    detailReportBand.DataSource = report.DataSource;
    detailReportBand.DataMember = dataMember;

    // Add a header to the detail report.
    ReportHeaderBand detailReportHeader = new ReportHeaderBand();

    XRTable tableHeader = new XRTable();
    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;

    // Adjust the table width.
    tableHeader.BeforePrint += tableHeader_BeforePrint;

    // Create a detail band.
    XRTable tableDetail = new XRTable();
    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;

        new ExpressionBinding("BeforePrint", "Text", "[ProductName]"));
        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;

    // Adjust the table width.
    tableDetail.BeforePrint += tableDetail_BeforePrint;

    // 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:

View Example: How to Create a Report Bound to the SQL Data Source (WinForms)

View Example: How to dynamically generate a master-detail report (WPF)