Walkthrough Scripting and Subreports

ActiveReports allows you to use scripting to permit reports saved to an XML file to contain code. By including scripting when the RPX files are saved into XML, the reports later can be loaded, run and displayed directly to the viewer control without needing to use the designer.

This walkthrough illustrates how to use scripting when creating a subreport.

This walkthrough is split up into the following activities:

Adding two ActiveReports to a Visual Studio project

• Connecting the subreport to a data source

• Adding controls to each report to display the data

• Adding the scripting code for rptMain

• Adding the scripting code for rptSub

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

Alfreds Fiitterkiste

ORDERED: 3/25/1335

REQUIRED:

1 a/23/1335

5HIFFED: 1 a/3/1335

PRODUCT NAME

QUANT ÍTY

UNIT PRICE

discount

Rössle 5 a u-a ricra ul

15

345.60

5-325

Ch a tre j sa va la

21

S13.33

53.25

Epeges:d

2

S12.33

SO .25

ORDERED: 11.'3'1395

REQUIRED:

12/1/1935

SHIFFED: 1: 13/1395

PRODUCT NAME

QUANT ÍTY

UNIT FRICE

discount

Vag.a-sp'-aad

23

S 4 3.90

SO .33

ORDERED: 11/13/1335 REQUIRED: 12/25'1335 SHI FF ED: 11/21/1335

PRODUCT NAME

QUANT ÍTY

UNIT FRICE

discount

syr-jd

6

510.33

50.30

la^sa: sbb"

15

513.33

50.30

ORDERED: 2/15-1335 REQUIRED: 3/14/1335 SHI FF ED: 2/21/1333

product name

quantity

UNIT FRICE

discount

Raclette CojrdavauE

15

S 5 5.30

50.30

O g ¡i a I F ra r, id j le r g rJ ne Este

2

S13.30

50.20

ORDERED: 4/15/1333 REQUIRED: 5/27/1330 SHIPPED: 4/23/1333

PRODUCT NAME

QUANT íty

UNIT FRICE

discount

Grandira s Ec/sen berry Bp read

10

525.30

50.05

Rbssla 5 a j-e Tîra jí

2

545.00

50.30

ORDERED: 5/9/1335 REQUIRED: 0/S/199S SHIFFED: 5/13/1335

PRODUCT name

quantity

UNIT FRICE

discount

Escargots da Euur-gcgne

40

51325

50.05

Pista nmysD st

20

321.50

50.30

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. Click on Project > Add New Item.

3. Select ActiveReports file and rename the file rptMain.

4. Click Open.

5. Click on Project > Add New Item.

6. Select ActiveReports file and rename the file rptSub.

7. Click Open.

Connecting the subreport to a data source To connect the subreport 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 display the data To add controls to the reports

1. Add a GroupHeader/Footer section to rptMain.

2. Make the following changes to the group header:

o Change the name to ghCompanies o Change the DataField to CompanyName o Change the GroupKeepTogether property to All

3. Add a second GroupHeader/Footer section to rptMain.

4. Make the following changes to the group header:

o Change the name to ghOrders o Change the DataField to OrderDate o Change the GroupKeepTogether property to All

5. Add the following controls to rptMain, naming them as indicated:

Control

DataField

Name

Text/Caption

Section Location

Control

DataField

Name

Text/Caption

Section Location

TextBox

CompanyName txtCompanyName Company Name

ghCompanies O, O

TextBox

OrderDate

txtOrderDate

Order Date

ghOrders

l, O

TextBox

RequiredDate

txtRequiredDate

Required Date

ghOrders

3.S,

O

TextBox

ShippedDate

txtShippedDate

Shipped Date

ghOrders

S.S,

O

Label

(Empty string)

lblOrderDate

Ordered:

ghOrders

O, O

Label

(Empty string)

lblRequiredDate

Required:

ghOrders

2.S,

O

Label

(Empty string)

lblShippedDate

Shipped:

ghOrders

4.B7S, O

Subreport

(Empty string)

Subreport1

(Empty string)

Detail

O, O

Add a GroupHeader/Footer section to rptSub. Make the following changes to the group header: o Change the name to ghOrderDetails o Change the DataField to [order details].orderID Add the following controls to the GroupHeader section:

Control

Label Label Label Label

Line

Line

Line

Name lblUnitPrice lblDiscount lblProductName lblQuantity

Line1

Line2

Line3

Text/Caption

Unit Price Discount Product Name Quantity

(Empty string)

(Empty string)

(Empty string)

Location

4.37S, O S.S, O O.O62S, O 3.2S, O X1 = 3.1B7S Yl = O X2 = 3.1B7S

Yl = O X2 = 4.312S Y2 = O.1B7S Xl = S.437S Yl = O X2 = S.437S Y2 = O.1B7S

Line

Line4

(Empty string)

X2 Y2

9. Add the following controls to the Detail section:

Control DataField

TextBox ProductName

TextBox Quantity order TextBox

details.UnitPrice

TextBox Discount

Name Text/Caption Misc Details txtProductNameProduct Name (Empty string) txtQuantity Quantity (Empty string)

OutputFormat = Currency OutputFormat = Currency txtUnitPrice txtDiscount

Unit Price

Discount

Line

(Empty string)

Line5

Line (Empty string) Line6

Line (Empty string) Line7

Line (Empty string) Line8

(Empty string) (Empty string)

(Empty string) (Empty string)

(Empty string) (Empty string)

(Empty string) (Empty string)

Adding the scripting code for rptMain To add scripting to the report

1. Click on the Edit Script icon on the report toolbar.

2. Add the following scripting code.

The following example shows what the scripting code looks like:

0.1875

Location

X1 = 3.1875 Y1 = 0 X2 = 3.1875 Y2 = 0.1875 X1 = 4.3125 Y1 = 0 X2 = 4.3125 Y2 = 0.1875 X1 = 5.4375 Y1 = 0 X2 = 5.4375 Y2 = 0.1875 X1 = 0

private string getDatabasePath() {

Microsoft.Win32.RegistryKey regKey = Microsoft.Win3 2.Registry.LocalMachine; regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports.NET\\Sample return (string)regKey.GetValue("");

public void ActiveReport_ReportStart() {

string m_dbPath = getDatabasePath();

string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_dbPath string sqlString = "Select * from orders inner join customers on orders.customer customerid order by CompanyName, OrderDate"; DataDynamics.ActiveReports.DataSources.

OleDBDataSource m_ds = new DataDynamics.ActiveReports.DataSources.OleDI m_ds.ConnectionString = m_cnnString; m_ds.SQL = sqlString; rpt.DataSource = m_ds;

public void Detail_Format() {

DataDynamics.ActiveReports.ActiveReport rptSub = new DataDynamics.ActiveReports. rptSub.LoadLayout(System.Windows.Forms.Application.StartupPath + @"\RPX Files\Or ((SubReport)rpt.Sections["Detail"].Controls["SubReport1"]).ReportName); ((SubReport)rpt.Sections["Detail"].Controls["SubReport1"]).Report = rptSub;

3. Click OK to continue.

Adding the scripting code for rptSub To add scripting to the report

1. Click on the Edit Script icon on the report toolbar.

2. Add the following scripting code.

The following example shows what the scripting code looks like:

int cnt;

public void Detail {

3. Click OK to continue.

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.

Was this article helpful?

0 -1

Post a comment