ActiveReports Developer 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:
- Adding a main report and a subreport to a Visual Studio project
- Connecting the main report to a data source
- Adding controls to the main report to display data and contain the subreport
- Adding controls to the subreport to display data
- Adding code to save the current record's CategoryID for use in the subreport's SQL query
- Adding code to create an instance of the subreport
- Adding code to assign a data source for the subreport
- Viewing the report
Note: This walkthrough uses tables from the NWind database. By default, in ActiveReports Developer, the NWind.mdb file is located in the [User Documents folder]\ComponentOne Samples\ActiveReports Developer 7\Samples\Data folder. |
When you complete this walkthrough you get a layout that looks similar to the following at design time and at runtime.
Design Time Layout
Runtime Layout
To add an ActiveReport to the Visual Studio project
- Create a new Visual Studio project.
- From the Project menu, select Add New Item.
- In the Add New Item dialog that appears, select ActiveReports 7 Section Report (code-based) and in the Name field, rename the file as rptMain.
- Click the Add button to open a new section report in the designer.
- From the Project menu, select Add New Item.
- In the Add New Item dialog that appears, select ActiveReports 7 Section Report (code-based) and in the Name field, rename the file as rptSub.
- Click the Add button to open a second new section report in the designer.
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the Parent Report (rptMain) to a data source
- On the detail section band, click the Data Source Icon.
- 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.
- Once the connection string field is populated, in the Query field, enter the following SQL query.
SQL Query Copy Code SELECT * FROM Categories - Click OK to save the data source and return to the report design surface.
To create a layout for the Parent Report (rptMain)
- In the Report Explorer, select the report and in the Properties window, set the PrintWidth property to 5.75.
- On the design surface, select the detail section and in the Properties window, set the CanShrink property to True to eliminate white space.
- 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 - From the toolbox, drag the following controls onto the detail section and in the Properties window, set the properties as follows.
Property Name Property Value Name txtCategoryID1 DataField CategoryID Visible False Property Name Property Value Name txtCategoryName1 DataField CategoryName Location 1.15, 0.05 in Property Name Property Value Name lblCategoryName Text CategoryName: Location 0, 0.05 in Size 1.15, 0.2 in Font Bold True Property Name Property Value Name lblProducts Text Products: Location 2.4, 0.05 in Font Bold True 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)
- 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. - 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.
- 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
- At the top left of the code view for the report, click the drop-down arrow and select (rptMain Events).
- 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.
- 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() |
- Click in the gray area below rptMain to select it.
- Click the events icon in the Properties Window to display available events for the report.
- Double-click ReportStart. This creates an event-handling method for the report's ReportStart event.
- 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)
- Back in design view of the Parent report (rptMain), double-click the detail section. This creates the Detail_Format event handler.
- 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 |
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; |
- Click the preview tab to view the report at design time.
OR
- Open the report in the Viewer. See Using the Viewer for further information.