ActiveReports 8 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Layout > Subreports with Run-Time Data Sources |
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. By default, in ActiveReports, the NWind.mdb file is located in the [User Documents folder]\ComponentOne Samples\ActiveReports 8\Samples\Data folder. |
When you complete this walkthrough you get a layout that looks similar to the following at design time and at runtime.
To add an ActiveReport to the Visual Studio project
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the Parent Report (rptMain) to a data source
SQL Query |
Copy Code
|
---|---|
SELECT * FROM Categories |
To create a layout for the Parent Report (rptMain)
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 |
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)
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. |
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
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#
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)
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
OR