[]
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 tutorial illustrates how to modify the subreport record source from the data in the parent report to retrieve the correct information.
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.
Once the connection string field is populated, in the Query field, enter the following SQL query.
SELECT * FROM Categories
Click OK to save the data source and return to the report design surface.
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 |
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 |
---|---|
TextBox1 | |
Name | txtCategoryID1 |
Data Field | CategoryID |
Visible | True |
TextBox2 | |
Name | txtCategoryName1 |
Data Field | CategoryName |
Label1 | |
Name | lblCategoryName |
Text | CategoryName: |
Label2 | |
Name | lblProducts |
Text | Products: |
Subreport | |
Name | SubReport1 |
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 |
type=info
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 |
On the design surface, select the detail section section and in the Properties panel, set the BackColor to 'White Smoke'
type=info
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 the following controls to the detail section and in the Properties panel, set the properties as follows.
Property Name | Property Value |
---|---|
TextBox1 | |
DataField | ProductName |
Name | txtProductName |
TextBox2 | |
DataField | UnitPrice |
Name | txtUnitPrice |
OutputFormat | $#,##0.00 (or select Currency in the dialog) |
type=warning
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.
VB Code:
The following example shows what the code for the method looks like.
Private rpt As rptSub
Private childDataSource As New GrapeCity.ActiveReports.Data.OleDBDataSource()
rpt = New rptSub()
C# Code:
The following example shows what the code for the method looks like.
private rptSub rpt;
private GrapeCity.ActiveReports.Data.OleDBDataSource childDataSource = new GrapeCity.ActiveReports.Data.OleDBDataSource();
rpt = new 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:
The following example shows what the code for the method looks like.
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
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.