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 do not render PageHeader/Footer sections, so you can delete them to save on processing time. |
This walkthrough illustrates how to set up a bound subreport by setting the Report property of the Subreport control to the child report. This walkthrough also demonstrates 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 a data source
- Adding controls to display the data
- Adding the code needed to save the current record's CategoryID to use in the subreport's SQL query
- Adding the code needed to create a new data source for the subreport
- Viewing the report
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.
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 rptParent.
- Click Open.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptChild.
- Click Open.
Connecting the parent report to a data source
To connect the parent report to a data source
- Click the gray report Datasource icon in the Detail section to open the report Datasource dialog.
- Click on Build.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next.
- Click the ellipsis button to browse for the access path to the NorthWind database. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select * from categories".
- 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 rptParent, setting the properties as indicated:
Control DataField Name Text Location Label lblProducts Products 1.15, 0.25 Label lblCategoryName Category Name: 0, 0 TextBox CategoryName txtCategoryName CategoryName 1.15, 0 Subreport subReport1 1.15, 0.5 - Add the following controls to the Detail section of rptChild, setting the properties as indicated:
Control DataField Name Text Location TextBox ProductName txtProductName Product Name 1.15, 0 Label lblProductName Product Name: 0, 0 - Set the CanShrink property of the Detail section of rptChild to True
Adding the code needed to save the current record's categoryID
Note: The Fields collection should never be accessed outside the DataInitialize and FetchData events.
To write the code in Visual Basic
- Right-click in any section of the design window of rptParent, and click on View Code to display the code view for the report. At the top left of the code view for rptParent, click the drop-down arrow and select rptParent events. At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for rptParent's FetchData event. Add code to the handler to:
- Save the current record's categoryID to use in the subreport's SQL query
To write the code in C#
- Click the gray area below rptParent to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click FetchData. This creates an event-handling method for the FetchData event of rptParent. Add code to the handler to:
- Save the current record's categoryID to use in the subreport's SQL query
The following example shows what the code for the method looks like.
'Visual Basic
Dim categoryIDString As String Private Sub rptParent_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics _ .ActiveReports.ActiveReport3.FetchEventArgs) Handles MyBase.FetchData
categoryIDString = Me.Fields("CategoryID").Value
End Sub//C#
string categoryIDString;
private void rptParent_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport3.
FetchEventArgs eArgs)
{
categoryIDString = Fields["CategoryID"].Value.ToString();
}
Adding the code to create a new data source for the subreport
To write the code in Visual Basic or C#
- Double-click the Detail section of rptParent to create an event-handling method for the Detail Format event of rptParent. Add code to the handler to:
- Create a new DataDynamics OleDBDataSource
- Set the connection string of the new data source
- Set the SQL query for the new data source
- Set the data source of the subreport 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 Detail1.Format Dim rpt As New rptChild() Dim childDataSource As New DataDynamics.ActiveReports.DataSources.OleDBDataSource() childDataSource.ConnectionString = CType(Me.DataSource, DataDynamics.ActiveReports.OleDBDataSource).ConnectionString childDataSource.SQL = "Select * from products where categoryID = " + categoryIDString rpt.DataSource = childDataSource Me.SubReport1.Report = rpt End Sub //C# private void detail_Format(object sender, System.EventArgs eArgs) { rptChild rpt = new rptChild(); DataDynamics.ActiveReports.DataSources.OleDBDataSource childDataSource = new DataDynamics. ActiveReports.DataSources.OleDBDataSource(); childDataSource.ConnectionString = ((DataDynamics.ActiveReports.OleDBDataSource)this.DataSource).ConnectionString; childDataSource.SQL = "Select * from products where categoryID = " + categoryIDString; rpt.DataSource = childDataSource; this.subReport1.Report = rpt;
}
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 of rptParent. 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. |