Walkthrough: Parameters with Subreports
Parameters can be used with subreports to connect the subreport to the parent report. By setting a parameter for the field that links the parent report to the subreport, the parent report can pass the information to the subreport through the parameters.
Note Subreports will not render PageHeader/Footer sections.
This walkthrough illustrates how to set up a subreport using parameters to link the parent report to the subreport.
This walkthrough is split up into the following activities:
- Adding two ActiveReports to a Visual Studio project
- Connecting the parent report to a data source
- Connecting the child report to a data source using parameters
- Adding controls to display the data
- Adding the code needed to link the subreport to the current record's supplierID
- Adding the code to set the subreport's ShowParametersUI property to False
- Viewing the report
To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb).
When you have finished this walkthrough, you will have a report that looks similar to the following.
Adding two ActiveReports to a Visual Studio project
To add two ActiveReports to a Visual Studio project
- Open a new project in Visual Studio.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptParent.
- Click Open.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptChild.
- Click Open.
Connecting the parent report to a data source
To connect the parent report to a data source
- Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.
- Click on Build...
- Select Microsoft Jet 4.0 OLE DB Provider and click Next >>
- Click on the ellipsis to browse for the access path to NWind.mdb. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select * from suppliers order by country".
- Click OK to return to the report design surface.
Connecting the child report to a data source using parameters
To connect the child report to a data source
- Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.
- Click on Build...
- Select Microsoft Jet 4.0 OLE DB Provider and click Next >>
- Click on the ellipsis to browse for the access path to NWind.mdb. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "SELECT * FROM products INNER JOIN categories ON products.categoryid = categories.categoryid WHERE products.supplierID =<%SupplierID%>".
- Click OK to return to the report design surface.
Adding controls to display the data
To add controls to the reports
- Add a GroupHeader/Footer section to rptParent.
- Make the following changes to the group header:
- Change the name to ghSuppliers
- Change the DataField property to Country
- Add the following controls to rptParent, naming them as indicated:
Control |
DataField |
Name |
Text/Caption |
Section |
Location |
TextBox |
Country |
txtCountry |
Country |
GroupHeader |
0, 0 |
TextBox |
CompanyName |
txtCompanyName |
Company Name |
Detail |
0.0625, 0.0625 |
TextBox |
ContactName |
txtContactName |
Contact Name |
Detail |
2.312, 0.0625 |
TextBox |
Phone |
txtPhone |
Phone |
Detail |
4.562, 0.0625 |
Subreport |
(Empty string) |
Subreport1 |
(Empty string) |
Detail |
0.0625, 0.312 |
- Add a GroupHeader/Footer section to rptChild.
- Make the following changes to the group header:
- Change the name to ghProducts
- Change the DataField property to CategoryName
- Add the following controls to rptChild, naming them as indicated:
Control |
DataField |
Name |
Text/Caption |
Section |
Location |
TextBox |
CategoryName |
txtCategoryName |
Category Name |
GroupHeader |
0.0625, 0.0625 |
TextBox |
ProductName |
txtProductName |
Product Name |
Detail |
0.0625, 0.0625 |
Adding the code needed to link the subreport to the current record's supplierID
To write the code in Visual Basic
- Right-click in any section of the design window of rptParent, and click on View code to display the code view for the report. At the top left of the code view for rptParent, click the drop-down arrow and select Detail. At the top right of the code window, click the drop-down arrow and select Format. This creates an event-handling method for rptParent's Detail_Format event. Add code to the handler to:
- Link the subreport to the current record's supplierID
To write the code in C#
- Click on the Detail section of rptParent to select the section. Click on the events icon in the Properties window to display available events for the report. Double-click Format. This creates an event-handling method for rptParent's Detail_Format event. Add code to the handler to:
- Link the subreport to the current record's supplierID
The following example shows what the code for the method looks like.
' Visual Basic
Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
Detail.Format
Dim rpt As New rptChild()
Me.SubReport1.Report = rpt
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
rptChild rpt = new rptChild();
this.SubReport1.Report = rpt;
}
Adding the code to set the subreport's ShowParametersUI property to False
To write the code in Visual Basic
- Right-click in any section of the design surface of rptChild, and click on View code to display the code view for the report. At the top left of the code view for rptChild, click the drop-down arrow and select (Base Class 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 rptChild's ReportStart event. Add code to the handler to:
- Set the subreport's ShowParametersUI property to False
To write the code in C#
- Click in the gray area below rptChild to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click ReportStart. This creates an event-handling method for rptChild's ReportStart event. Add code to the handler to:
- Set the subreport's ShowParametersUI property to False
The following example shows what the code for the method looks like.
' Visual Basic
Private Sub rptChild_ReportStart(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.ReportStart
Me.ShowParameterUI = False
End Sub
//C#
private void rptChild_ReportStart(object sender, System.EventArgs eArgs)
{
this.ShowParameterUI = false;
}
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports WinForm Viewer for help.
Samples | Walkthroughs | Parameters
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.