Back to Devexpress

Create a Report with a Subreport (Runtime Sample)

xtrareports-403396-feature-guide-to-devexpress-reports-reporting-api-create-reports-in-code-create-a-report-with-subreport.md

latest12.4 KB
Original Source

Create a Report with a Subreport (Runtime Sample)

  • Feb 18, 2026
  • 5 minutes to read

This topic demonstrates how to create a report that contains an XRSubreport control at runtime.

Create a Main Report and Assign a Data Source

The following code creates a report with data from the Northwind database grouped by the Category field. The XRSubreport control is located in the Group Footer band. The XRSubreport.ReportSource property specifies the report instance that the XRSubreport control renders.

For more information on report bands, review the following help topic: Introduction to Banded Reports.

csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.XtraPrinting;
using DevExpress.XtraCharts;
using DevExpress.XtraReports.Parameters;
// ...
private XtraReport CreateMainReport() {
    XtraReport report = new XtraReport() {
        Bands = {
            new GroupHeaderBand() {
                GroupFields = {
                    new GroupField("CategoryID")
                },
                Controls = {
                    new XRLabel() {
                        ExpressionBindings = {
                            new ExpressionBinding("BeforePrint", "Text", "[CategoryName]")
                        },
                        BoundsF = new RectangleF(0,0,300,25),
                        Font = new Font(new FontFamily("Arial"),12,FontStyle.Bold)
                    },
                    new XRLabel() {
                        ExpressionBindings = {
                            new ExpressionBinding("BeforePrint", "Text", "[Description]")
                        },
                        BoundsF = new RectangleF(0,50,300,25),
                        Font = new Font(new FontFamily("Arial"),9)
                    },
                    new XRPictureBox() {
                        ExpressionBindings = {
                            new ExpressionBinding("BeforePrint", "ImageSource", "[Picture]")
                        },
                        BoundsF = new RectangleF(500,0,150,50),
                        Sizing = ImageSizeMode.ZoomImage
                    },
                    new XRLabel() {
                        Text = "Product Name",
                        BoundsF = new RectangleF(50,100,400,25),
                        Font = new Font(new FontFamily("Arial"),9,FontStyle.Bold)
                    },
                    new XRLabel() {
                        Text = "Qty Per Unit",
                        BoundsF = new RectangleF(450,100,100,25),
                        Font = new Font(new FontFamily("Arial"),9,FontStyle.Bold)
                    },
                    new XRLabel() {
                        Text = "Unit Price",
                        BoundsF = new RectangleF(550,100,100,25),
                        TextAlignment = TextAlignment.TopRight,
                        Font = new Font(new FontFamily("Arial"),9,FontStyle.Bold)
                    },
                }
            },
            new DetailBand() {
                Controls = {
                    new XRLabel() {
                        ExpressionBindings = {
                            new ExpressionBinding("BeforePrint", "Text", "[ProductName]")
                        },
                        BoundsF = new RectangleF(50,0,400,25),
                        Font = new Font(new FontFamily("Arial"),9)
                    },
                    new XRLabel() {
                        ExpressionBindings = {
                            new ExpressionBinding("BeforePrint", "Text", "[QuantityPerUnit]")
                        },
                        BoundsF = new RectangleF(450,0,100,25),
                        Font = new Font(new FontFamily("Arial"),9)
                    },
                    new XRLabel() {
                        ExpressionBindings = {
                            new ExpressionBinding("BeforePrint", "Text", "[UnitPrice]")
                        },
                        BoundsF = new RectangleF(550,0,100,25),
                        Font = new Font(new FontFamily("Arial"),9),
                        TextAlignment = TextAlignment.TopRight,
                        TextFormatString = "{0:c2}"
                    }
                },
                HeightF = 25
            },
            new GroupFooterBand() {
                Controls = {
                    new XRSubreport(){
                        ReportSource = CreateSubReport(),
                        GenerateOwnPages = true,
                        ParameterBindings = {
                            new ParameterBinding("subreportCategory",null,"Products.CategoryID")
                        }
                    }
                },
                PageBreak = PageBreak.BeforeBand
            }
        },
        DataSource = CreateDataSource(),
        DataMember = "Products"
    };
    return report;
}
vb
Imports System.IO
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraCharts
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.Parameters
Imports DevExpress.XtraReports.UI
' ...
Private Function CreateMainReport() As XtraReport
    Dim report As New XtraReport()
    report.DataSource = CreateDataSource()
    report.DataMember = "Products"

    ' Creates a Group Header Band.
    Dim groupHBand As New GroupHeaderBand()
    groupHBand.GroupFields.Add(New GroupField("CategoryID"))
    Dim label1 As New XRLabel() With {
        .BoundsF = New RectangleF(0, 0, 300, 25),
        .Font = New Font(New FontFamily("Arial"), 12, FontStyle.Bold)
    }
    Dim label2 As New XRLabel() With {
        .BoundsF = New RectangleF(0, 50, 300, 25),
        .Font = New Font(New FontFamily("Arial"), 9)
    }
    Dim pictureBox1 As New XRPictureBox With {
        .BoundsF = New RectangleF(500, 0, 150, 50),
        .Sizing = ImageSizeMode.ZoomImage
    }
    Dim label3 As New XRLabel() With {
        .Text = "Product Name",
        .BoundsF = New RectangleF(50, 100, 400, 25),
        .Font = New Font(New FontFamily("Arial"), 9, FontStyle.Bold)
    }
    Dim label4 As New XRLabel() With {
        .Text = "Qty Per Unit",
        .BoundsF = New RectangleF(450, 100, 100, 25),
        .Font = New Font(New FontFamily("Arial"), 9, FontStyle.Bold)
    }
    Dim label5 As New XRLabel() With {
        .Text = "Unit Price",
        .BoundsF = New RectangleF(550, 100, 100, 25),
        .TextAlignment = TextAlignment.TopRight,
        .Font = New Font(New FontFamily("Arial"), 9, FontStyle.Bold)
    }
    label1.ExpressionBindings.Add(
        New ExpressionBinding("BeforePrint", "Text", "[CategoryName]"))
    label2.ExpressionBindings.Add(
        New ExpressionBinding("BeforePrint", "Text", "[Description]"))
    pictureBox1.ExpressionBindings.Add(
        New ExpressionBinding("BeforePrint", "ImageSource", "[Picture]"))
    groupHBand.Controls.AddRange(
        New XRControl() {label1, label2, pictureBox1, label3, label4, label5})

    ' Creates a Detail Band.
    Dim dtlBand As New DetailBand()
    dtlBand.HeightF = 25
    Dim label6 As New XRLabel() With {
        .BoundsF = New RectangleF(50, 0, 400, 25),
        .Font = New Font(New FontFamily("Arial"), 9)
    }
    Dim label7 As New XRLabel() With {
        .BoundsF = New RectangleF(450, 0, 100, 25),
        .Font = New Font(New FontFamily("Arial"), 9)
    }
    Dim label8 As New XRLabel() With {
        .BoundsF = New RectangleF(550, 0, 100, 25),
        .Font = New Font(New FontFamily("Arial"), 9),
        .TextAlignment = TextAlignment.TopRight,
        .TextFormatString = "{0:c2}"
    }
    label6.ExpressionBindings.Add(
        New ExpressionBinding("BeforePrint", "Text", "[ProductName]"))
    label7.ExpressionBindings.Add(
        New ExpressionBinding("BeforePrint", "Text", "[QuantityPerUnit]"))
    label8.ExpressionBindings.Add(
        New ExpressionBinding("BeforePrint", "Text", "[UnitPrice]"))
    dtlBand.Controls.AddRange(
        New XRControl() {label6, label7, label8})

    ' Creates a Group Footer Band with a Subreport.
    Dim groupFBand As New GroupFooterBand()
    groupFBand.PageBreak = PageBreak.BeforeBand
    Dim subreport1 As New XRSubreport() With {
        .ReportSource = CreateSubReport(),
        .GenerateOwnPages = True
    }
    subreport1.ParameterBindings.Add(
        New ParameterBinding("subreportCategory",
                             Nothing,
                             "Products.CategoryID"))
    groupFBand.Controls.Add(subreport1)

    report.Bands.AddRange(
        New Band() {groupHBand, dtlBand, groupFBand})

    Return report
End Function

Create a Subreport

The following code creates a report assigned to the ReportSource property of the XRSubreport control located in the main report.

The report includes the XRChart control that displays data related to the current group in the main report.

csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.XtraPrinting;
using DevExpress.XtraCharts;
using DevExpress.XtraReports.Parameters;
// ...
private XtraReport CreateSubReport() {
    XtraReport report = new XtraReport()
    {
        Bands = {
            new DetailBand() {
                Controls = {
                    new XRChart(){
                        BoundsF = new RectangleF(0,0,900,650),
                        DataMember = "Products",
                        Series = {
                            new Series(){
                                ArgumentDataMember = "ProductName"
                            }
                        }
                    }
                }
            }
        },
        Parameters = {
            new Parameter(){
                Name = "subreportCategory",
                Type = typeof(System.Int32)
            }
        },
        Landscape = true,
        DataSource = CreateDataSource(),
    };
    var chart = report.Bands[0].Controls[0] as XRChart;
    chart.Parameters.Add(new XRControlParameter("chartCategory", report.Parameters[0]));
    chart.Series[0].FilterString = "CategoryID=?chartCategory";
    chart.Series[0].ValueDataMembers.AddRange(new string[] { "UnitPrice"});
    return report;
}
vb
Imports System.IO
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraCharts
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.Parameters
Imports DevExpress.XtraReports.UI
' ...
Private Function CreateSubReport() As XtraReport
    Dim report As New XtraReport()
    ' Add a Detail Band with Chart.
    Dim dtlBand As New DetailBand()
    Dim chartControl1 As New XRChart() With {
        .BoundsF = New RectangleF(0, 0, 900, 650),
        .DataMember = "Products"
    }
    chartControl1.Series.Add(
        New Series() With {
        .ArgumentDataMember = "ProductName"})
    dtlBand.Controls.Add(chartControl1)
    report.Bands.Add(dtlBand)
    ' Add a parameter.
    report.Parameters.Add(New Parameter() With {
                          .Name = "subreportCategory",
                          .Type = GetType(System.Int32)
                          })

    report.Landscape = True
    report.DataSource = CreateDataSource()
    ' Configure the chart.
    chartControl1.Parameters.Add(New XRControlParameter("chartCategory", report.Parameters(0)))
    chartControl1.Series(0).FilterString = "CategoryID=?chartCategory"
    chartControl1.Series(0).ValueDataMembers.AddRange(New String() {"UnitPrice"})

    Return report
End Function

Result

The resulting report is shown in the picture below. It contains a table and a chart for each category.

View Example: Use Subreports to Add a Chart