ActiveReports for .NET 3 Online Help Request technical support
Walkthrough: Parameters with Subreports
See Also
User Guide > Samples and Walkthroughs > Walkthroughs > Standard Edition Walkthroughs > Advanced > Parameters Walkthroughs > Walkthrough: Parameters with Subreports

Glossary Item Box

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:

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

  1. Open a new project in Visual Studio.
  2. From the Project menu, select Add New Item.
  3. Select ActiveReports 3.0 File and rename the file rptParent.
  4. Click Open.
  5. From the Project menu, select Add New Item.
  6. Select ActiveReports 3.0 File and rename the file rptChild.
  7. Click Open.

Connecting the parent report to a data source

To connect the parent report to a data source

  1. Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  2. Select the "OLE DB" tab.
  3. Click on Build.
  4. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  5. Click the ellipsis button to browse for the access path to the NorthWind database. Click Open once you have selected the appropriate access path.
  6. Click OK to continue.
  7. In the Query field, type "Select * from suppliers order by country".
  8. 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

  1. Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  2. Select the "OLE DB" tab.
  3. Click on Build.
  4. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  5. Click the ellipsis button to browse for the access path to the NorthWind database. Click Open once you have selected the appropriate access path.
  6. Click OK to continue.
  7. In the Query field, type "SELECT * FROM products INNER JOIN categories ON products.categoryid = categories.categoryid WHERE products.supplierID =<%SupplierID%>".
  8. Click OK to return to the report design surface.

Adding controls to display the data

To add controls to the reports

  1. Add a GroupHeader/Footer section to rptParent by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
  2. Make the following changes to the group header:
    • Change the Name property to ghSuppliers
    • Change the DataField property to Country
  3. 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

     

  4. Add a GroupHeader/Footer section to rptChild by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
  5. 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
  6. 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#

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#

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

  1. Add the ActiveReports viewer control to a Windows Form.
  2. 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.

See Also

©2009. All Rights Reserved.