ActiveReports allows reports to contain any number of child reports by using the Subreport control. Child reports, or subreports, are executed each time the parent section (i.e. the section in which the Subreport control is placed) is printed.
Subreports will not render PageHeader/Footer sections, so you can delete them to save on processing time. |
This walkthrough illustrates how to set up a subreport bound to an XML DataSource by setting the Subreport control's Report property to the child report and how to modify the subreport record source from the data in the parent report to retrieve the correct information.
This walkthrough is split up into the following activities:
- Adding two ActiveReports to a Visual Studio project
- Connecting the parent report to an XML data source
- Adding controls to display the data
- Adding the code to create a new XML data source, setting the NodeList for the current record, and setting the new data source equal to the subreport's data source
- Viewing the report
To complete the walkthrough, you must have access to the XML Customer database (Customer.xml).
When you have finished this walkthrough, you will have a report that looks similar to the following.
Adding two ActiveReports to a Visual Studio project
To add two ActiveReports to a Visual Studio project
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptMain.
- Click Open.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptSub.
- Click Open.
Connecting the parent report to a data source
To connect the parent report to a data source
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- In the Data source combo box at the top of the dialog, select XML.
- Click the ellipsis button beside File URL to browse for the access path to Customer.xml. Click Open once you have selected the appropriate access path. (The default installation path is C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Samples\CSharp\XMLSample\customer.xml.)
- In the Recordset Pattern field, type "//CUSTOMER".
- Click OK to return to the report design surface.
Adding controls to display the data
To add controls to the reports
- Add the following controls to the Detail section of rptMain, naming them as indicated:
Control DataField Name Text/ReportName Location Label lblCustomerName Customer Name: 0, 0 in TextBox NAME txtCustomerName NAME 1.2, 0 in Label lblOrders Orders: 0.25, 0.25 in Subreport ctlSubreport rptSub 0.75, 0.5 in - Add the following controls to the Detail section of rptSub, naming them as indicated:
Control DataField Name Text Location TextBox TITLE txtTitle TITLE 0, 0 in TextBox PRICE txtPrice PRICE 3.5, 0 in
Adding the code to create a new data source
To write the code in Visual Basic or C#
- Double-click the Detail section in rptMain, to create an event-handling method for the report's Detail_Format event. 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
- Set the subreport's data source equal to the new data source
The following example shows what the code for the method looks like.
'Visual Basic Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Detail.Format 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) Me.ctlSubreport.Report = New rptSub Me.ctlSubreport.DataSource = xmlDS End Sub //C# private void detail_Format(object sender, System.EventArgs eArgs) { 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); this.ctlSubreport.Report = new rptSub(); this.ctlSubreport.DataSource = xmlDS; }
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |