ActiveReports 6 Online Help
Subreports with XML Data
See Also Send comments on this topic.
ActiveReports 6 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Subreport Walkthroughs > Subreports with XML Data

Glossary Item Box

Using XML data requires some setup that is different from other types of data. This walkthrough illustrates how to set up a subreport bound to the XML DataSource in the parent report.

This walkthrough is split up into the following activities:

Tip: For basic steps like viewing a report, please see the Basic Data Bound Reports walkthrough.

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

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

To connect the parent report to a data source

  1. Add two ActiveReports to a Visual Studio project, naming them rptMain and rptSub.
  2. Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
  3. In the tabs along the top of the dialog, select XML.
  4. Click the ellipsis button beside File URL to browse for the access path to Customer.xml and click Open. (The default installation path is C:\Program Files\GrapeCity\ActiveReports 6\Data\customer.xml, or C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\customer.xml on a 64-bit Windows operating system).
  5. In the Recordset Pattern field, enter //CUSTOMER.
  6. Click OK to return to the report design surface.
  7. In the Report Explorer, expand the Fields node, then the Bound node, then Document, then Customer to see the fields.

To add controls to rptMain to display data

  1. Set the Height property of the page header section to 0.3.
  2. Set the CanShrink property of the Detail section to True to eliminate white space.
  3. Drag the following controls from the ActiveReports Toolbox onto the indicated section of rptMain, setting the properties as indicated. 

ShowControls for rptMain

Control Section Text DataField Miscellaneous Location Size
Label PageHeader Orders by Customer Font size = 14
Alignment = Center
0, 0 in 6.5, 0.25 in
Label Detail Customer Name: Bold 0, 0 in 1.2, 0.198 in
TextBox Detail NAME   1.2, 0 in 2, 0.198 in
Label Detail Orders: Bold 1.2, 0.25 in 1, 0.198 in
Subreport Detail     2.3, 0.25 in 4, 1 in

To add controls to rptSub to display data

  1. Set the CanShrink property of the Detail section of rptSub to True to eliminate white space.
  2. Set the BackColor property of the Detail section to LightSteelBlue to distinguish the subreport from the main report.
    Tip: Even if you do not want colors in your finished reports, using background colors on subreports can help in troubleshooting layout issues.
  3. Right-click the PageHeader or PageFooter section and select Delete. Subreports do not render these sections, so deleting them saves processing time.
  4. Add the following controls to the Detail section of rptSub, setting the properties as indicated:

ShowControls for rptSub

Control DataField Name Miscellaneous Location Size
TextBox TITLE txtTitle 0, 0 in 2.9, 0.198 in
TextBox PRICE txtPrice Alignment = Right
OutputFormat = $#,##0.00 (or select Currency in the dialog)
3, 0 in 1, 0.198 in

To add code to create a new instance of the subreport

Warning: Do not create a new instance of the subreport in the Format event. Doing so creates a new subreport each time the section Format code is run, which uses a lot of memory.

ShowTo write the code in Visual Basic

  1. Right-click the design surface of rptMain and select View Code.
  2. At the top left of the code view of the report, click the drop-down arrow and select (rptMain Events).
  3. At the top right of the code window, click the drop-down arrow and select ReportStart. This creates an event-handling method for the ReportStart event.
  4. Add code to the handler to create an instance of rptSub.

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

Visual Basic.NET code. Paste JUST ABOVE the ReportStart event. Copy Code
Dim rpt As rptSub
Visual Basic.NET code. Paste INSIDE the ReportStart event. Copy Code
rpt = New rptSub

ShowTo write the code in C#

  1. Click in the gray area below rptMain to select it.
  2. Click the events icon in the Properties Window to display available events for the report.
  3. Double-click ReportStart. This creates an event-handling method for the report's ReportStart event.
  4. Add code to the handler to create a new instance of rptSub.

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

C# code. Paste JUST ABOVE the ReportStart event. Copy Code
private rptSub rpt;
C# code. Paste INSIDE the ReportStart event. Copy Code
rpt = new rptSub();

To add code to pass a subset of the parent report's data to the subreport

  1. Double-click in the detail section of the design surface of rptMain to create a detail_Format event.
  2. Add code to the handler to:
    • Create a new DataDynamics XMLDataSource
    • Type cast the new data source as rptMain's data source and set the NodeList to the "ORDER/ITEM" field
    • Display rptSub in the subreport control
    • Pass the new data source to the subreport

ShowTo write the code in Visual Basic

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

Visual Basic.NET code. Paste INSIDE the Format event. Copy Code
    Dim xmlDS As New DataDynamics.ActiveReports.DataSources.XMLDataSource
    xmlDS.NodeList = CType(CType(Me.DataSource, DataDynamics.ActiveReports.DataSources.XMLDataSource).Field("ORDER/ITEM", True), System.Xml.XmlNodeList)
    rpt.DataSource = xmlDS
    Me.SubReport1.Report = rpt

ShowTo write the code in C#

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

C# code. Paste INSIDE the Format event. Copy Code
   DataDynamics.ActiveReports.DataSources.XMLDataSource xmlDS = new DataDynamics.ActiveReports.DataSources.XMLDataSource();
   xmlDS.NodeList = (System.Xml.XmlNodeList)((DataDynamics.ActiveReports.DataSources.XMLDataSource) this.DataSource).Field("ORDER/ITEM", true);
   rpt.DataSource = xmlDS;
   this.SubReport1.Report = rpt;

See Also