Walkthrough Master Detail Reports with Subreports

ActiveReports allows you to create Master Detail reports by using subreports to retrieve and group data. This walkthrough illustrates how to create a Master Detail report with subreports. This walkthrough is split up into the following activities:

• Adding two ActiveReports to a Visual Studio project

• Connecting the Master report to a data source

• Connecting the Detail report to a data source

• Adding controls to the report to contain data

• Adding code to retrieve the subreport data at run time

• Viewing the report

To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb). When you have completed this walkthrough, you will have a report that looks similar to the following.

Order Date ShioName ShioDate

OrdEr ID

13353

OrderlD 13330

LILA- 1 1/25/1994

âupennen^tto 12;DitB{] AIM

Product ID

Prod-uctlD

ProductNanie Quantity

G jnnmlbärcnan

Ship Address Ship Cotrntrv Freiohl

Venezuas S12.'75

Quantity

Discount JO«

Dis tout S3.15

Order Date

Ship Name

Ship Date

Ship Address

Ship CoLrntrv

Frei ohl

1 1'13 '1334

Bnn a pa'

11 ¿21/1994

12, ua deî

France

313.13

12:33:3-3 Al.-i

12:3-3:33 Ali'

B-cucnes

OrderlD

Product ID

ProductNanie

Q L 3 n ti ty

Discouit

13331

54

U rtli r=

15

SG.33

Adding two ActiveReports to a Visual Studio project To add two ActiveReports to your project

1. Open a new project in Visual Studio.

2. Click on Project > Add New Item.

3. Select ActiveReports file and rename the file rptMaster.

4. Click Open.

5. Click on Project > Add New Item.

6. Select ActiveReports file and rename the file rptDetail.

7. Click Open.

Connecting the Master report to a data source To connect the Master 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".

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

Connecting the Detail report to a data source To connect the Detail 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 [order details] inner join products on [order details].productid = products.productID where [order details].orderID = <%OrderID%>".

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

Adding controls to the report to contain data To add controls to the report

1. Add the following controls to the Detail section of rptMaster:

Control

DataField Name

Text/Caption

Misc Details

Location

Label

(Empty string) lblOrderDate

Order Date

(Empty string)

0.0625, 0.0625

Label

(Empty string) lblShipName

Ship Name

(Empty string)

1.125, 0.0625

Label

(Empty string) lblShipDate

Shipped Date

(Empty string)

2.187, 0.0625

Label

(Empty string) lblShipAddress Ship Address

(Empty string)

3.25, 0.0625

Label

(Empty string) lblShipCountry Ship Country

(Empty string)

4.437, 0.0625

Label

(Empty string) lblFreight

Freight

(Empty string)

5.687, 0.0625

Textbox

OrderDate txtOrderDate

Order Date

(Empty string)

0.0625, 0.3125

Textbox

ShipName txtShipName

Ship Name

(Empty string)

1.125, 0.3125

Textbox

ShippedDate txtShippedDate Shipped Date

(Empty string)

2.1875, 0.3125

Textbox

ShipAddress txtShipAddress Ship Address

(Empty string)

3.25, 0.3125

Textbox

ShipCountry txtShipCountry Ship Country

(Empty string)

4.4375, 0.3125

OutputFormat =

Textbox Freight txtFreight Freight Currency 5.687, 0.3125

Subreport (Empty string) ctlSubreport (Empty string) (Empty string) 0, 0.5625

Label (Empty string) lblWhiteLine (Empty string) Background Color = White 0, 1.75

2. Add the following controls to the Detail section of rptDetail:

Control

DataField

Name

Text/Caption

Misc Details

Location

Textbox

OrderID

txtOrderID

Order ID

(Empty string)

G.G625,

G.25

Textbox

ProductName

txtProductName Product Name

(Empty string)

2.3125,

G.25

Textbox

products.ProductID txtProductID

Product ID

(Empty string)

1.1B75,

G.25

Textbox

Quantity

txtQuantity

Quantity

(Empty string)

3.4375,

G.25

Textbox

Discount

txtDiscount

Discount

OutputFormat = Currency

4.5625,

G.25

Label

(Empty string)

lblOrderID

Order ID

(Empty string)

G.G625,

G

Label

(Empty string)

lblProductID

Product ID

(Empty string)

1.1B75,

G

Label

(Empty string)

lblProductName Product Name

(Empty string)

2.3125,

G

Label

(Empty string)

lblQuantity

Quantity

(Empty string)

3.4275,

G

Label

(Empty string)

lblDiscount

Discount

(Empty string)

4.5625,

G

Adding code to retrieve subreport data during run time To write the code in Visual Basic

• Right-click in any section of the design window of rptMaster, and click on View Code to display the code view for the report. At the top left of the code view for rptMaster, 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 rptMaster's Detail_Format event. Add code to the handler to:

• Retrieve subreport data at run time To write the code in C#

• Click in the Detail section of rptMaster 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 rptMaster's Detail_Format event. Add code to the handler to:

• Retrieve subreport data at run time

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

End Sub

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

rptDetail rpt = new rptDetail(); this.ctlSubReport.Report = rpt;

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