ActiveReports allows 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:
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.MDB (on a 64-bit Windows operating system, a copy is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\NWIND.MDB).
When you have finished this walkthrough, you will have a report that looks similar to the following.
SQL Query |
Copy Code |
---|---|
SELECT * FROM Categories |
Label Properties
Size | Font Size | Name | Miscellaneous | Text | Location |
---|---|---|---|---|---|
5.75, 0.25 in | 14 | lblProductsbyCategory | Align = Center | Products by Category | 0, 0 in |
Detail section controls
Control | Miscellaneous | Name | Text | Location |
---|---|---|---|---|
Label | Bold Size = 1.15, 0.2 in |
lblCategoryName | Category Name: | 0, 0.05 in |
TextBox | DataField = CategoryName | txtCategoryName1 | 1.15, 0.05 in | |
Label | Bold | lblProducts | Products: | 2.4, 0.05 in |
SubReport | Size = 2.25, 1 in | SubReport1 | 3.5, 0.05 in | |
TextBox |
DataField = CategoryID Visible = False |
txtCategoryID1 |
Tip: Even if you do not want colors in your finished reports, using background colors on subreports can help in troubleshooting layout issues. |
TextBox properties
Size | DataField | Name | Text | Location |
---|---|---|---|---|
2.25, 0.198 in | ProductName | txtProductName | Product Name | 0, 0 in |
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 DataDynamics.ActiveReports.DataSources.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 DataDynamics.ActiveReports.DataSources.OleDBDataSource childDataSource = new DataDynamics.ActiveReports.DataSources.OleDBDataSource(); |
C# code. Paste INSIDE the ReportStart event. |
Copy Code |
---|---|
rpt = new 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, DataDynamics.ActiveReports.DataSources.OleDBDataSource).ConnectionString childDataSource.SQL = "SELECT * FROM Products WHERE CategoryID = " + Me.txtCategoryID1.Value.ToString rpt.DataSource = childDataSource Me.SubReport1.Report = rpt |
To write the code in C#
C# code. Paste INSIDE the Format event. |
Copy Code |
---|---|
childDataSource.ConnectionString = ((DataDynamics.ActiveReports.DataSources.OleDBDataSource)this.DataSource).ConnectionString; childDataSource.SQL = "SELECT * FROM Products WHERE CategoryID = " + this.txtCategoryID1.Value.ToString(); rpt.DataSource = childDataSource; this.SubReport1.Report = rpt; |