Skip to main content
A newer version of this page is available. .

Create a Master-Detail Report (Runtime Sample)

  • 8 minutes to read

This tutorial demonstrates the main needed steps to create a data-aware report in code.

Add Required References and Create a Report

The following code creates a master-detail report and displays its Print Preview.

To use this code in your application, include all necessary libraries in the References list of the project.

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.Configuration;
using DevExpress.XtraReports.UI;
using System.Drawing;
using System.Windows;
// ...
        public void CreateReport() {
            // Create an empty report.
            XtraReport report = new XtraReport();

            // Bind the report to a data source.

            // Create a master report.
            CreateReportHeader(report, "Products by Categories");

            // Create a detail report.
            SqlDataSource ds = report.DataSource as SqlDataSource;
            CreateDetailReport(report, ds.Queries[0].Name + "." + ds.Relations[0].Name);

            // Publish the report.

Bind the Report to Data

The following code creates a SqlDataSource with a master-detail relation and binds the report to it.

To learn how to bind your report to other data source types, see Providing Data to Reports.

private void BindToData(XtraReport report) {
    // Create a data source.
    Access97ConnectionParameters connectionParameters = new Access97ConnectionParameters("../../nwind.mdb", "", "");
    DevExpress.DataAccess.Sql.SqlDataSource ds = new DevExpress.DataAccess.Sql.SqlDataSource(connectionParameters);

    // Create an SQL query to access the master table.
    CustomSqlQuery queryCategories = new CustomSqlQuery();
    queryCategories.Name = "queryCategories";
    queryCategories.Sql = "SELECT * FROM Categories";

    // Create an SQL query to access the detail table.
    CustomSqlQuery queryProducts = new CustomSqlQuery();
    queryProducts.Name = "queryProducts";
    queryProducts.Sql = "SELECT * FROM Products";

    // Add the queries to the data source collection.
    ds.Queries.AddRange(new SqlQuery[] { queryCategories, queryProducts });

    // Create a master-detail relation between the queries.
    ds.Relations.Add("queryCategories", "queryProducts", "CategoryID", "CategoryID");

    // Assign the data source to the report.
    report.DataSource = ds;
    report.DataMember = "queryCategories";

Create a Master Report Section

The following code creates content for the master section of the report.

In particular, it adds a ReportHeaderBand and DetailBand to the collection of report bands and adds various report controls to these bands.

The label placed onto the Report Header displays static text, and the label placed onto the Detail band is bound to the “CategoryName” field from the report’s data source.

Use the XRControl.ExpressionBindings property to provide data to the report.

private void CreateReportHeader(XtraReport report, string caption) {
    // Create a report title.
    XRLabel label = new XRLabel();
    label.Font = new Font("Tahoma", 12, System.Drawing.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 void CreateDetail(XtraReport report) {
    // Create a new label with the required settings. bound to the CategoryName data field.
    XRLabel labelDetail = new XRLabel();
    labelDetail.Font = new Font("Tahoma", 10, System.Drawing.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 following code creates a detail section in the report.

Next, it creates new Report Header and Detail bands at the detail level and adds a table control to each of these bands.

The cells of the table placed in the Detail band are bound to the relational table of the report’s data source. This table is assigned different styles for displaying odd and even rows from the data source.

The width of both tables equals the effective page width of the report.

private 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, System.Drawing.FontStyle.Bold);
    tableHeader.Padding = 10;
    tableHeader.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;

    XRTableCell cellHeader1 = new XRTableCell();
    cellHeader1.Text = "Product Name";
    XRTableCell cellHeader2 = new XRTableCell();
    cellHeader2.Text = "Unit Price";
    cellHeader2.TextAlignment = DevExpress.XtraPrinting.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 = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;

    XRTableCell cellDetail1 = new XRTableCell();
    XRTableCell cellDetail2 = new XRTableCell();
    cellDetail2.TextAlignment = DevExpress.XtraPrinting.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;

    // 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 void AdjustTableWidth(XRTable table) {
    XtraReport report = table.RootReport;
    table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;

void tableHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
    AdjustTableWidth(sender as XRTable);

void tableDetail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
    AdjustTableWidth(sender as XRTable);

Preview and Publish the Report

The following code illustrates the easiest approach to publish reports in a desktop application (WinForms or WPF).

private void PublishReport(XtraReport report) {
    ReportPrintTool printTool = new ReportPrintTool(report);