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.
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.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\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.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptParent.
- Click Open.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 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 gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click on Build.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next.
- Click the ellipsis button to browse for the access path to the NorthWind database. 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 gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click on Build.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next.
- Click the ellipsis button to browse for the access path to the NorthWind database. 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 by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the Name property to ghSuppliers
- Change the DataField property to Country
- Add the following controls to rptParent, setting the properties as indicated:
Control DataField Name Text Section Location Miscellaneous TextBox Country txtCountry Country GroupHeader 0, 0 Font size = 12 TextBox CompanyName txtCompanyName Company Name Detail 0, 0 Size = 1.94, 0.19 TextBox ContactName txtContactName Contact Name Detail 2, 0 Size = 1.9, 0.19 TextBox Phone txtPhone Phone Detail 4, 0 Size = 1, 0.19 Subreport Subreport1 Detail 0, 0.31 Size = 5, 1 - Add a GroupHeader/Footer section to rptChild by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the Name property to ghProducts
- Change the BackColor property to Silver
- Change the CanShrink property to True
- Change the DataField property to CategoryName
- Drag the following fields onto rptChild, setting the properties as indicated:
DataField Name Text Section Size Location CategoryName txtCategoryName Category Name GroupHeader 2.5, 0.19 0, 0 ProductName txtProductName Product Name Detail 2.5, 0.19 0, 0
Adding the code needed to link the subreport to the current record's supplierID
To write the code in Visual Basic or C#
- Double-click in the detail section of rptParent. 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 Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.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 or C#
- Double-click in the gray area below rptChild. 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
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 Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |