ActiveReports 6 Online Help
Script for Subreports
Show AllShow All
Hide AllHide All

ActiveReports allows you to use scripting to permit reports saved to an XML file to contain code. By including scripting when reports 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:

Tip: For basic steps like adding a report to a Visual Studio project and viewing a report, please see the Basic Data Bound Reports walkthrough.

To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.MDB (on a 64-bit Windows operating system, a copy is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\NWIND.MDB).

When you have finished this walkthrough, you will have a report that looks similar to the following.

To temporarily connect the main report to a data source

  1. Add an ActiveReports 6 File (xml-based) to a Visual Studio project and rename it rptMain.
    Note: The following steps are just for convenience so that the fields list in the Report Explorer can be populated at design time.
  2. Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
  3. On the OLE DB tab, next to Connection String, click the Build button.
  4. In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button.
  5. Click the ellipsis (...) button to browse to the Northwind database. Click Open once you have selected the appropriate access path.
  6. Click OK to close the window and fill in the Connection String field.
  7. In the Query field, enter the following SQL query.
    SQL Query
    Copy Code
    SELECT * FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID ORDER BY CompanyName, OrderDate
  8. Click OK to save the data source and return to the report design surface.

To temporarily connect the subreport to a data source

  1. Add a second ActiveReports 6 (xml-based) File to the project and rename it rptSub.
    Note: The following steps are just for convenience so that the fields list in the Report Explorer can be populated at design time.
  2. Right-click the PageHeader or PageFooter section and select Delete. Subreports do not render these sections, so deleting them saves processing time.
  3. Click in the grey area below the report to select it, and in the Properties Window, change the report's ShowParameterUI property to False. This prevents the subreport from requesting a parameter from the user.
  4. Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
  5. Click the Build button.
  6. In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button. 
  7. Click the ellipsis button to browse to the NorthWind database. Click Open once you have selected the appropriate access path.
  8. Click OK to close the window and fill in the Connection String field.
  9. In the Query field, enter the following SQL query.
    SQL Query
    Copy Code
    Select * from [order details] inner join products on [order details].productid = products.productid
  10. Click OK to return to the report design surface.

To add controls to rptMain to display data

  1. Right-click on the design surface of rptMain and select Insert then Group Header/Footer to add group header and footer sections to the report.
  2. In the Properties Window, make the following changes to the group header:
    • Name: ghCompanies
    • BackColor: LemonChiffon
    • CanShrink: True
    • DataField: CompanyName
    • GroupKeepTogether: All
    • KeepTogether: True
  3. In the Report Explorer, expand the Fields node, then the Bound node. Drag the following field onto ghCompanies and set the properties as indicated.

    Group header ghCompanies field

  4. Add a second GroupHeader/Footer section to rptMain.
  5. Make the following changes to the group header:
    • Name: ghOrders
    • BackColor: LightYellow
    • CanShrink: True
    • DataField: OrderDate
    • GroupKeepTogether: All
    • KeepTogether: True
  6. Drag the following fields and controls onto ghOrders and set the properties as indicated.

    ghOrders controls

  7. Change the CanShrink property of the detail section to True.
  8. Drag the following control onto the detail section and set the properties as indicated.

    Detail section control

To add controls to rptSub

  1. Right-click on the design surface of rptSub and select Insert then Group Header/Footer to add group header and footer sections to the report.
  2. Make the following changes to the group header:
    • Name: ghOrderDetails
    • BackColor: LightSteelBlue
    • CanShrink: True
    • DataField: OrderID
  3. Add four label controls to ghOrderDetails and set the properties as indicated.

    ghOrderDetails labels

  4. Add four line controls to ghOrderDetails and set the properties as indicated.

    ghOrderDetails line controls

  5. Make the following changes to the detail section:
    • BackColor: Gainsboro
    • CanShrink: True
  6. In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the properties as indicated.

    Detail section fields

  7. Add four line controls to the detail section and set the properties as follows (or copy and paste them from ghOrderDetails):

    Detail section lines

To embed script in the main report

  1. Change the ScriptLanguage property for the report to the appropriate scripting language. The default setting is C#.
  2. Click the Script tab located below the report designer to access the scripting editor.

  3. Embed script to set the data source for the main report and pass data into the subreport.

The following example shows what the script looks like.

To write the script in Visual Basic.NET

To write the script in C#

To write the code to load the xml-based report into the ActiveReports viewer

  1. From the Visual Studio toolbox, drag the ActiveReports viewer control onto the Windows Form and set its Dock property to Fill.
  2. Double-click the title bar of the Windows Form containing the viewer to create a Form_Load event.
  3. Add code to load the RPX into a generic ActiveReport and display it in the viewer.

The following example shows what the code for the method looks like.

To write the code in Visual Basic.NET

To write the code in C#

See Also

Related Sections

Concepts