When setting up embedded subreports in ActiveReports, the principles are the same as when setting up simple subreports but are applied to the child-grandchild reports.
Note: Subreports will not render PageHeader/Footer sections, so you can delete them to save on processing time.
This walkthrough illustrates how to set up embedded subreports.
This walkthrough is split up into the following activities:
- Adding three ActiveReports to a Visual Studio project
- Connecting each report to a data source
- Adding controls to each report to display the data
- Adding the code needed to set the subreport controls equal to their corresponding reports
- Setting the subreports' 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 three ActiveReports to a Visual Studio project
To add three 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 rptEmployees.
- Click Open.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptOrders.
- Click Open.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptCustomers.
- Click Open.
Connecting rptEmployees to a data source
To connect the 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 employees".
- Click OK to return to the report design surface.
Connecting rptOrders to a data source
To connect the 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 orders inner join [order details] on orders.orderID = [order details].orderID where orders.employeeID = <%employeeID%>".
- Click OK to return to the report design surface.
Connecting rptCustomers to a data source
To connect the 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 customers where customerID = '<%CustomerID%>' ".
- 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 rptEmployees
- Make the following changes to the group header:
- Change the name to ghEmployees
- Change the DataField property to EmployeeID
- Add the following controls to rptEmployees, naming them as indicated:
Control |
DataField |
Name |
Text/Caption |
Section |
Location |
TextBox |
EmployeeID |
txtEmployeeID |
Employee ID |
GroupHeader |
0, 0 |
TextBox |
Extension |
txtExtension |
Extension |
GroupHeader |
3.375, 0 |
TextBox |
LastName |
txtLastName |
Last Name |
GroupHeader |
1.125, 0 |
TextBox |
FirstName |
txtFirstName |
First Name |
GroupHeader |
2.25, 0 |
Label |
(Empty string) |
lblEmployeeID |
Employee ID |
GroupHeader |
0, 0 |
Label |
(Empty string) |
lblExtension |
Extension |
GroupHeader |
3.375, 0 |
Label |
(Empty string) |
lblLastName |
Last Name |
GroupHeader |
1.125, 0 |
Label |
(Empty string) |
lblFirstName |
First Name |
GroupHeader |
2.25, 0 |
Subreport |
(Empty string) |
subOrders |
(Empty string) |
Detail |
0, 0 |
- Add the following controls to the Detail section of rptOrders, naming them as indicated:
Control |
DataField |
Name |
Text/Caption |
Location |
TextBox |
OrderDate |
txtOrderDate |
Order Date |
1.9375, 0.25 |
TextBox |
Quantity |
txtQuantity |
Quantity |
4.4375, 0.25 |
TextBox |
orders.OrderID |
txtOrderID |
Order ID |
0, 0.25 |
TextBox |
ProductID |
txtProductID |
Product ID |
3, 0.25 |
Label |
(Empty string) |
lblOrderDate |
Order Date: |
1.9375, 0 |
Label |
(Empty string) |
lblQuantity |
Quantity: |
4.4375, 0 |
Label |
(Empty string) |
lblOrderID |
Order ID: |
0, 0 |
Label |
(Empty string) |
lblProductID |
Product ID: |
3, 0 |
Subreport |
(Empty string) |
subCustomers |
(Empty string) |
0, 0.5 |
- Add the following controls to the Detail section of rptCustomers, naming them as indicated:
Control |
DataField |
Name |
Text/Caption |
Location |
TextBox |
CompanyName |
txtCompanyName |
Company Name |
0, 0.25 |
TextBox |
ContactName |
txtContactName |
Contact Name |
1.25, 0.25 |
TextBox |
Phone |
txtPhone |
Phone |
2.3125, 0.25 |
Label |
(Empty string) |
lblCompanyName |
Company Name |
0, 0 |
Label |
(Empty string) |
lblContactName |
Contact Name |
1.25, 0 |
Label |
(Empty string) |
lblPhone |
Phone |
2.3125, 0 |
Adding the code needed to set subOrders equal to rptOrders in rptEmployees
To write the code in Visual Basic
- Right-click in any section of the design window of rptEmployees, and click on View Code to display the code view for the report. At the top left of the code view for rptEmployees, 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 rptEmployees' Detail_Format event. Add code to the handler to:
- Set subOrders equal to rptOrders
To write the code in C#
- Click in the Detail section of rptEmployees to select the section. Click on the events icon in the Properties window to display available events for the section. Double-click Format. This creates an event-handling method for rptEmployees' Detail_Format event. Add code to the handler to:
- Set subOrders equal to rptOrders
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 rptOrders()
Me.subOrders.Report = rpt
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
rptOrders rpt = new rptOrders();
subOrders.Report = rpt;
}
Adding the code needed to set subCustomers equal to rptCustomers in rptOrders
To write the code in Visual Basic
- Right-click in any section of the design window of rptOrders, and click on View Code to display the code view for the report. At the top left of the code view for rptOrders, 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 rptOrders' Detail_Format event. Add code to the handler to:
- Set subCustomers equal to rptCustomers
To write the code in C#
- Click in the Detail section of rptOrders to select the section. Click on the events icon in the Properties window to display available events for the section. Double-click Format. This creates an event-handling method for rptOrders' Detail_Format event. Add code to the handler to:
- Set subCustomers equal to rptCustomers
The following example shows what the code for the method looks like.
' Visual Basic
Private Sub ghOrders_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
ghOrders.Format
Dim rpt As New rptCustomers()
Me.subCustomers.Report = rpt
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
rptCustomers rpt = new rptCustomers();
subCustomers.Report = rpt;
}
Setting the ShowParametersUI property to false
To set the ShowParametersUI property to false
- Click in the dark gray area underneath rptCustomers to select the ActiveReport.
- In the properties window, change the ShowParametersUI property to False.
- In the same way, set the ShowParametersUI property to False for rptOrders.
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
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.