Nested Subreports

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.

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 subreport's 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.

EmployeelD Last Name First Name Extension

1 Davolio Nancy 5407

EmployeelD Last Name First Name Extension

1 Davolio Nancy 5407

Order ID:

Order Date:

Product ID

Quantity:

12 : [13 : [13 AH

13

20

Co mo any Nana

Co nta et Na ma P M o ne

Bon app

L a u re nse 31.24.45.« Lebihan

Order ID:

Order Date:

Product ID

Quantity:

10340

11123'1334 12:33:33 AM

41

Co nnp a ny Na ma

Cp nta pt Na me Phone

E p n a p p'

Laurence 91.24.45.40 Leo nan

Order ID:

Order Date:

Product ID

Quantity:

13343

1 112 3.1994 1 2:33:33 AM

43

40

Cpmpany Kama

Cpntapt Name Phpne

Epn app

Lsirenas 31.24.45.40 Leo ¡nan

Order ID:

Order Date:

Product ID

Quantity:

13258

S/1 7/1334 12:30:33 AM

%

53

Cpmpany Name

ContaptName Phone

E m st I- a nd a!

Rp and Vende- 7075-3425

Order tD:

Order Date:

Product ID

Quantity:

1:253

3/1 7/1394 12:3 3:3-3 AM

£

05

Co mpa ny Na me Contact Name Phone Ernst hands Rp and Mendel 70 75-3425

Co mpa ny Na me Contact Name Phone Ernst hands Rp and Mendel 70 75-3425

Adding three ActiveReports to a Visual Studio project To add three ActiveReports to a Visual Studio project

1. Open a new project in Visual Studio.

2. Click on Project > Add New Item.

3. Select ActiveReports file and rename the file rptEmployees.

4. Click Open.

5. Click on Project > Add New Item.

6. Select ActiveReports file and rename the file rptOrders.

7. Click Open.

8. Click on Project > Add New Item.

9. Select ActiveReports file and rename the file rptCustomers. 10. Click Open.

Connecting rptEmployees to a data source To connect the report to a data source

1. Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.

3. Select Microsoft Jet 4.0 OLE DB Provider and click Next >>

4. Click on the ellipsis to browse for the access path to NWind.mdb. Click Open once you have selected the appropriate access path.

5. Click OK to continue.

6. In the Query field, type "Select * from employees".

7. 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 on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.

3. Select Microsoft Jet 4.0 OLE DB Provider and click Next >>

4. Click on the ellipsis to browse for the access path to NWind.mdb. Click Open once you have selected the appropriate access path.

5. Click OK to continue.

6. In the Query field, type "Select * from orders inner join [order details] on orders.orderlD = [order details].orderID where orders.employeelD = <%employeeID%>".

7. Click OK to return to the report design surface.

Connecting rptCustomers to a data source To connect the report to a data source

1. 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

1. Add a GroupHeader/Footer section to rptEmployees

2. Make the following changes to the group header:

o Change the name to ghEmployees o Change the DataField property to EmployeeID

3. Add the following controls to rptEmployees, naming them as indicated:

Control TextBox TextBox TextBox TextBox Label Label Label Label Subreport

DataField

EmployeeID Extension LastName FirstName (Empty string) (Empty string) (Empty string) (Empty string) (Empty string)

Name txtEmployeeID txtExtension txtLastName txtFirstName lblEmployeeID lblExtension lblLastName lblFirstName subOrders

Text/Caption Section

Employee ID GroupHeader

Extension Last Name First Name Employee ID Extension Last Name First Name (Empty string)

GroupHeader GroupHeader GroupHeader GroupHeader GroupHeader GroupHeader GroupHeader Detail

Location

3.375, 0 1.125, 0 2.25, 0 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.OrderlD

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 Del Dim rpt As New rptOrders() Me.subOrders.Report = rpt

End Sub

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 Dim rpt As New rptCustomers() Me.subCustomers.Report = rpt

private void Detail_Format(object sender, System.EventArgs eArgs) {

rptCustomers rpt = new rptCustomers(); subCustomers.Report = rpt;

Setting rptCustomers' ShowParametersUI property to false To set rptCustomers' ShowParametersUI property to false

1. Click in the dark gray area underneath rptCustomers to select the ActiveReport.

2. In the properties window, change the ShowParametersUI property to 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 WinForm Viewer for help.

0 0

Post a comment