Back to Devexpress

Create a Master-Detail Report

xtrareports-115727-feature-guide-to-devexpress-reports-reporting-api-create-reports-in-code-create-a-master-detail-report-runtime-sample.md

latest14.8 KB
Original Source

Create a Master-Detail Report

  • Feb 18, 2026
  • 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.

csharp
using DevExpress.DataAccess.Sql;
using DevExpress.Drawing;
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;
}
vb
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.Drawing
' ...
Public Shared Function CreateReport(ByVal dataSource As Object) As XtraReport
    Dim ds As SqlDataSource = TryCast(dataSource, SqlDataSource)
    If ds Is Nothing Then
        Return New XtraReport()
    End If

    ' Create an empty report.
    Dim report As 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
End Function

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.

csharp
using DevExpress.DataAccess.Sql;
using DevExpress.Drawing;
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 DXFont("Tahoma", 12, DXFontStyle.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 DXFont("Tahoma", 10, DXFontStyle.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);
}
vb
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.Drawing
' ...
Private Shared Sub CreateReportHeader(ByVal report As XtraReport, ByVal caption As String)
    ' Create a report title.
    Dim label As New XRLabel()
    label.Font = New DXFont("Tahoma", 12, DXFontStyle.Bold)
    label.Text = caption
    label.WidthF = 300F

    ' Create a report header and add the title to it.
    Dim reportHeader As New ReportHeaderBand()
    report.Bands.Add(reportHeader)
    reportHeader.Controls.Add(label)
    reportHeader.HeightF = label.HeightF
End Sub

Private Shared Sub CreateDetail(ByVal report As XtraReport)
    ' Create a new label bound to the CategoryName data field.
    Dim labelDetail As New XRLabel()
    labelDetail.Font = New DXFont("Tahoma", 10, DXFontStyle.Bold)
    labelDetail.WidthF = 300.0F

    ' 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.
    Dim detailBand As New DetailBand()
    detailBand.Height = labelDetail.Height
    detailBand.KeepTogetherWithDetailReports = True
    report.Bands.Add(detailBand)
    labelDetail.TopF = detailBand.LocationFloat.Y + 20F
    detailBand.Controls.Add(labelDetail)
End Sub

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.

csharp
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using DevExpress.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 DXFont("Tahoma", 10, DXFontStyle.Bold);
    tableHeader.Padding = new PaddingInfo(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 DXFont("Tahoma", 10);
    tableDetail.Padding = new PaddingInfo(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);
}
vb
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.Drawing
' ...
Private Shared Sub CreateDetailReport(ByVal report As XtraReport, ByVal dataMember As String)
    ' Create a detail report band and bind it to data.
    Dim detailReportBand As New DetailReportBand()
    report.Bands.Add(detailReportBand)
    detailReportBand.DataSource = report.DataSource
    detailReportBand.DataMember = dataMember

    ' Add a header to the detail report.
    Dim detailReportHeader As New ReportHeaderBand()
    detailReportBand.Bands.Add(detailReportHeader)

    Dim tableHeader As New XRTable()
    tableHeader.BeginInit()
    tableHeader.Rows.Add(New XRTableRow())
    tableHeader.Borders = BorderSide.All
    tableHeader.BorderColor = Color.DarkGray
    tableHeader.Font = New DXFont("Tahoma", 10, DXFontStyle.Bold)
    tableHeader.Padding = New PaddingInfo(10)
    tableHeader.TextAlignment = TextAlignment.MiddleLeft

    Dim cellHeader1 As New XRTableCell()
    cellHeader1.Text = "Product Name"
    Dim cellHeader2 As 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.
    AddHandler tableHeader.BeforePrint, AddressOf tableHeader_BeforePrint
    tableHeader.EndInit()

    ' Create a detail band.
    Dim tableDetail As New XRTable()
    tableDetail.BeginInit()
    tableDetail.Rows.Add(New XRTableRow())
    tableDetail.Borders = BorderSide.Left Or BorderSide.Right Or BorderSide.Bottom
    tableDetail.BorderColor = Color.DarkGray
    tableDetail.Font = New DXFont("Tahoma", 10)
    tableDetail.Padding = New PaddingInfo(10)
    tableDetail.TextAlignment = TextAlignment.MiddleLeft

    Dim cellDetail1 As New XRTableCell()
    Dim cellDetail2 As 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 })

    Dim detailBand As New DetailBand()
    detailBand.Height = tableDetail.Height
    detailReportBand.Bands.Add(detailBand)
    detailBand.Controls.Add(tableDetail)

    ' Adjust the table width.
    AddHandler tableDetail.BeforePrint, AddressOf tableDetail_BeforePrint
    tableDetail.EndInit()

    ' Create and assign different odd and even styles.
    Dim oddStyle As New XRControlStyle()
    Dim evenStyle As 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"
End Sub

Private Shared Sub AdjustTableWidth(ByVal table As XRTable)
    Dim report As XtraReport = table.RootReport
    table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right
End Sub

Private Shared Sub tableHeader_BeforePrint(ByVal sender As Object,
                                           ByVal e As System.ComponentModel.CancelEventArgs)
    AdjustTableWidth(TryCast(sender, XRTable))
End Sub

Private Shared Sub tableDetail_BeforePrint(ByVal sender As Object,
                                           ByVal e As System.ComponentModel.CancelEventArgs)
    AdjustTableWidth(TryCast(sender, XRTable))
End Sub

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)