ActiveReports 14 .NET Edition
ActiveReports 14 User Guide / Samples and Walkthroughs / Walkthroughs / Section Report Walkthroughs / Layout / Subreports with Run-Time Data Sources
In This Topic
    Subreports with Run-Time Data Sources
    In This Topic

    ActiveReports allows section reports to contain any number of child reports using the Subreport control. Child reports, or subreports, are executed each time the parent section (i.e. the section in which the Subreport control is placed) is processed. This walkthrough illustrates how to modify the subreport record source from the data in the parent report to retrieve the correct information.

    This walkthrough is split up into the following activities:

    Note: This walkthrough uses tables from the NWind database. The NWIND.mdb file can be downloaded from GitHub: ..\Samples14\Data\NWIND.mdb.

    When you complete this walkthrough you get a layout that looks similar to the following at design time and at run time.

    Design-Time Layout

    Subreport with run-time data sources - design time layout

    Subreport with run-time data sources - design time layout

    Run-Time Layout

    Subreport with run-time data sources - run time layout

    Subreport with run-time data sources - run time layout

    To add an ActiveReport to the Visual Studio project

    1. Create a new Visual Studio project.
    2. From the Project menu, select Add New Item.
    3. In the Add New Item dialog that appears, select ActiveReports 14 Section Report (code-based) and in the Name field, rename the file as rptMain.
    4. Click the Add button to open a new section report in the designer.
    5. From the Project menu, select Add New Item.
    6. In the Add New Item dialog that appears, select ActiveReports 14 Section Report (code-based) and in the Name field, rename the file as rptSub.
    7. Click the Add button to open a second new section report in the designer.

    See Quick Start for information on adding different report layouts.

    To connect the Parent Report (rptMain) to a data source

    1. On the detail section band, click the Data Source Icon.
      Data Source Icon
    2. In the Report Data Source dialog that appears, from the OLE DB tab, create a data source connection. See Bind Reports to a Data Source for further details.
    3. Once the connection string field is populated, in the Query field, enter the following SQL query.
      SQL Query
      Copy Code
      SELECT * FROM Categories
    4. Click OK to save the data source and return to the report design surface.

    To create a layout for the Parent Report (rptMain)

    1. In the Report Explorer, select the report and in the Properties window, set the PrintWidth property to 5.75.
    2. On the design surface, select the detail section and in the Properties window, set the CanShrink property to True to eliminate white space.
    3. From the toolbox, drag a Label control onto the pageHeader section and in the Properties window, set the properties as follows.
      Property Name Property Value
      Name lblProductsbyCategory
      Text Products by Category
      Location 0, 0 in
      Size 5.75, 0.25 in
      Font Size 14
      Alignment Center
    4. From the toolbox, drag the following controls onto the detail section and in the Properties window, set the properties as follows.

      TextBox1

      Property Name Property Value
      Name txtCategoryID1
      DataField CategoryID
      Visible False

      TextBox2

      Property Name Property Value
      Name txtCategoryName1
      DataField CategoryName
      Location 1.15, 0.05 in

      Label1

      Property Name Property Value
      Name lblCategoryName
      Text CategoryName:
      Location 0, 0.05 in
      Size 1.15, 0.2 in
      Font Bold True

      Label2

      Property Name Property Value
      Name lblProducts
      Text Products:
      Location 2.4, 0.05 in
      Font Bold True

      Subreport

      Property Name Property Value
      Name SubReport1
      Location 3.5, 0.05 in
      Size 2.25, 1 in

    To create a layout for the Child Report (rptSub)

    1. On the design surface, select the detail section and in the Properties window, set the following properties.
      Property Name Property Value
      CanShrink True
      BackColor AliceBlue
      Tip: Even if you do not want colors in your finished reports, using background colors on subreports can help in troubleshooting layout issues.
    2. On the design surface, right-click the pageHeader or pageFooter section and select Delete. Subreports do not render these sections, so deleting them saves processing time.
    3. From the toolbox, drag a TextBox control to the detail section and in the Properties window, set the following properties.
      Property Name Property Value
      DataField ProductName
      Name txtProductName
      Text Product Name
      Location 0, 0 in
      Size 2.25, 0.2 in

    To add code to create an instance of the subreport

    Warning: Do not create a new instance of the subreport in the Format event. Doing so creates a new subreport each time the section Format code is run, which uses a lot of memory.

    To write the code in Visual Basic

    1. At the top left of the code view for the report, click the drop-down arrow and select (rptMain Events).
    2. At the top right of the code window, click the drop-down arrow and select ReportStart. This creates an event-handling method for the report's ReportStart event.
    3. Add code to the handler to create a new instance of the subreport.

    The following example shows what the code for the method looks like.

    Visual Basic.NET code. Paste JUST ABOVE the ReportStart event.
    Copy Code
    Private rpt As rptSub
    Private childDataSource As New GrapeCity.ActiveReports.Data.OleDBDataSource()
    
    Visual Basic.NET code. Paste INSIDE the ReportStart event.
    Copy Code
    rpt = New rptSub()
    

    To write the code in C#

    1. Click in the gray area below rptMain to select it.
    2. Click the events icon in the Properties Window to display available events for the report.
    3. Double-click ReportStart. This creates an event-handling method for the report's ReportStart event.
    4. Add code to the handler to create a new instance of the subreport.

    The following example shows what the code for the method looks like.

    C# code. Paste JUST ABOVE the ReportStart event.
    Copy Code
    private rptSub rpt;
    private GrapeCity.ActiveReports.Data.OleDBDataSource childDataSource = new GrapeCity.ActiveReports.Data.OleDBDataSource();
    
    C# code. Paste INSIDE the ReportStart event.
    Copy Code
    rpt = new rptSub();
    

    To add code to assign a data source for the Child Report (rptSub)

    1. Back in design view of the Parent report (rptMain), double-click the detail section. This creates the Detail_Format event handler.
    2. Add code to the handler to:
      • Set the connection string for the OleDBDataSource for the subreport
      • Set the SQL query for the new data source and pass in the current record's CategoryID
      • Set the data source of the subreport to the data source
      • Assign rptSub to the SubReport control

    To write the code in Visual Basic

    The following example shows what the code for the method looks like.

    Visual Basic.NET code. Paste INSIDE the Format event.
    Copy Code
    childDataSource.ConnectionString = CType(Me.DataSource, GrapeCity.ActiveReports.Data.OleDBDataSource).ConnectionString
    childDataSource.SQL = "SELECT * FROM Products WHERE CategoryID = " + Me.txtCategoryID1.Value.ToString
    rpt.DataSource = childDataSource
    SubReport1.Report = rpt
    

    To write the code in C#

    C# code. Paste INSIDE the Format event.
    Copy Code
    childDataSource.ConnectionString = ((GrapeCity.ActiveReports.Data.OleDBDataSource)this.DataSource).ConnectionString;
    childDataSource.SQL = "SELECT * FROM Products WHERE CategoryID = " + this.txtCategoryID1.Value.ToString();
    rpt.DataSource = childDataSource;
    SubReport1.Report = rpt;
    

    To view the report

    • Click the preview tab to view the report at design time.

    OR