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.
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 code 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.
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 three ActiveReports to a Visual Studio project
To add three 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 rptEmployees.
- Click Open.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptOrders.
- Click Open.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 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 gray report DataSource icon in the Detail section to open the report DataSource dialog.
- 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 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 gray report DataSource icon in the Detail section to open the report DataSource dialog.
- 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 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 gray report DataSource icon in the Detail section to open the DataSource dialog.
- 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 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 by right-clicking on the design surface of the report and selecting Insert > Report Header/Footer.
- 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, setting the properties as indicated:
Control Section DataField Name Text Location TextBox GroupHeader EmployeeID txtEmployeeID Employee ID 0, 0.3 TextBox GroupHeader LastName txtLastName Last Name 3.375, 0.3 TextBox GroupHeader FirstName txtFirstName First Name 1.125, 0.3 TextBox GroupHeader Extension txtExtension Extension 2.25, 0.3 Label GroupHeader lblEmployeeID Employee ID 0, 0 Label GroupHeader lblLastName Last Name 1.125, 0 Label GroupHeader lblFirstName First Name 1.125, 0 Label GroupHeader lblExtension Extension 2.25, 0 Subreport Detail subOrders 0, 0 - Add the following controls to the Detail section of rptOrders, setting the properties as indicated.
Control DataField Name Text 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 lblOrderDate Order Date: 1.9375, 0 Label lblQuantity Quantity: 4.4375, 0 Label lblOrderID Order ID: 0, 0 Label lblProductID Product ID: 3, 0 Subreport subCustomers 0, 0.5 - Add the following controls to the Detail section of rptCustomers, setting the properties as indicated.
Control DataField Name Text 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 Company Name lblCompanyName Company Name 0, 0 Label Contact Name lblContactName Contact Name 1.25, 0 Label Phone lblPhone Phone 2.3125, 0
Adding code to set the subOrders control in rptEmployees equal to rptOrders
To write the code in Visual Basic or C#
- Double-click in the Detail section rptEmployees. 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 Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.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 code to set the subCustomers control in rptOrders equal to rptCustomers
To write the code in Visual Basic or C#
- Double-click in the in the Detail section of rptOrders to create 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 Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.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 beneath 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 of rptEmployees. 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. |