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

Glossary Item Box

By using the Subreport control, ActiveReports reports can contain any number of child reports. Child reports, or subreports, are executed each time the parent section (i.e. the section in which the Subreport control is placed) is printed.

Subreports will not render PageHeader/Footer sections, so you can delete them to save processing time.

This walkthrough illustrates how to set up a bound subreport by setting the Report property of the Subreport control to the child report. This walkthrough also demonstrates 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:

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 rptCustomers.
  4. Click Open.
  5. From the Project menu, select Add New Item.
  6. Select ActiveReports 3.0 File and rename the file rptOrders.
  7. Click Open.

Connecting rptCustomers to a data source

To connect the report to a data source

  1. Click the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  2. Click Build.
  3. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  4. Click the ellipsis to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
  5. Click OK to continue.
  6. In the Query field, type "SHAPE {SELECT CustomerID, CompanyName, Address FROM Customers} APPEND ({SELECT CustomerID, OrderID, Freight, OrderDate, EmployeeID FROM Orders} AS CustomerOrders RELATE CustomerID TO CustomerID)".
  7. Add "Provider=MSDataShape;" to the beginning of your Connection String and change "Provider=Microsoft.Jet.OLEDB.4.0;" to "Data Provider=Microsoft.Jet.OLEDB.4.0;". Your complete Connection String should look similar to: "Provider=MSDataShape;Data Provider=Microsoft.Jet.OLEDB.4.0;C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB;Persist Security Info=False".
  8. Click OK to return to the report design surface.

Connecting rptOrders to a data source

To connect the report to a data source

  1. Click the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  2. Click Build.
  3. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  4. Click the ellipsis to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
  5. Click OK to continue.
  6. In the Query field, type "SELECT OrderDate, OrderID, Freight, EmployeeID FROM Orders".
  7. Click OK to return to the report design surface.

Adding controls to display the data

To add controls to the reports

  1. Add the following controls to the detail section of rptCustomers and set the properties as indicated.

    Control DataField Name Text Location Miscellaneous
    TextBox CustomerID txtCustomerID Customer ID 0, 0 ForeColor=Blue; FontStyle=Bold 
    TextBox CompanyName txtCompanyName Company Name 1.0625, 0 ForeColor=Blue; FontStyle=Bold
    TextBox Address txtAddress Address 3.9375, 0 ForeColor=Blue; FontStyle=Bold
    Subreport CustomerOrders Subreport1 0, 0.5 Size= 6.5, 1

  2. Add a GroupHeader/Footer section to rptOrders by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
  3. Make the following changes to the group header:
    • Change the name to ghOrders
    • Change the DataField property to CustomerID
  4. Add the following controls to rptOrders and set the properties as indicated.

    Control Section Name DataField Text Location Miscellaneous
    Label GroupHeader lblOrderDate Order Date 0, 0 FontStyle=Bold; OutputFormat=MM/DD/YY
    Label GroupHeader lblOrderID Order ID 1.1875, 0 FontStyle=Bold
    Label GroupHeader lblFreight Freight 2.375, 0 FontStyle=Bold
    TextBox Detail txtOrderDate OrderDate Order Date 0, 0 0, 0
    TextBox Detail txtOrderID OrderID Order ID 1.1875, 0 0, 0
    TextBox Detail txtFreight Freight Freight 2.375, 0 0, 0
    TextBox Detail txtEmployeeID Employee ID Employee ID 5.9375, 0 0, 0
    Label Detail lblProcessed Processed by Employee ID #: 3.5625, 0 Font=Bold
    Line Detail line1 X1: 0

    X2: 6.5

    Y1: 0.25

    Y2: 0.25

    0, 0

Adding the code needed to set the subreport control equal to rptOrders

To write the code in Visual Basic or C#

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 of rptCustomers. 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.