Walkthrough: Bookmarks with Subreports
ActiveReports allows Bookmarks to be easily set up and used in subreports by adding code to the Detail_Format event of the parent and child reports.
This walkthrough illustrates how to set up and use Bookmarks in a subreport.
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 the report to contain data
- Adding the code needed to save the current record's CategoryID to use in the subreport's SQL query
- Adding the code to create a new data source, setting its connection string, setting its SQL query and setting the new data source equal to the subreport's data source.
- Adding code to the Detail_Format event for both reports to setup Bookmarks
- Viewing the report
- Viewing the Bookmarks collection with the report
- Adding special bookmarks at run time
To complete the walkthrough, you must have access to the NorthWind database (Nwind.mdb).
When you have completed 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.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptMainBM.
- Click Open.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptSubBM.
- Click Open.
Connecting the parent report to a data source
To connect the parent report to a data source
- Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.
- Click on Build...
- Select Microsoft Jet 4.0 OLE DB Provider and click Next >>
- Click on the ellipsis to browse for the access path to NWind.mdb. 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 the report to contain data
To add controls to the reports
- Add the following controls to the Detail section of rptMainBM:
Control |
DataField |
Name |
Text/Caption |
Location |
Label |
(Empty string) |
lblProducts |
Products |
1.0625, 0.25 |
Label |
(Empty string) |
lblCategoryName |
Category Name: |
0, 0 |
TextBox |
CategoryName |
txtCategoryName |
CategoryName |
1.06, 0 |
Subreport |
(Empty string) |
ctlSubreport |
(Empty string) |
1.0625, 0.5 |
- Add the following controls to the Detail section of rptSubBM:
Control |
DataField |
Name |
Text/Caption |
Location |
TextBox |
ProductName |
txtProductName |
ProductName |
1.187, 0.06 |
Label |
(Empty string) |
lblProductName |
Product Name: |
0.06, 0.06 |
Adding the code needed to save the current record's categoryID
To write the code in Visual Basic
- Right-click in any section of the design window of rptMainBM, and click on View Code to display the code view for the report. At the top left of the code view for rptMainBM, click the drop-down arrow and select (Base Class Events). At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for rptMainBM'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 in the gray area below rptMainBM 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 rptMainBM's FetchData event. 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 m_categoryID As String
Private Sub rptMainBM_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics _
.ActiveReports.ActiveReport.FetchEventArgs) Handles MyBase.FetchData
m_categoryID = Me.Fields("CategoryID").Value
End Sub
//C#
string m_categoryID;
private void rptMainBM_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport
.FetchEventArgs eArgs)
{
m_categoryID = Fields["CategoryID"].Value.ToString();
}
Adding the code to create a new data source
To write the code in Visual Basic
- Right-click in any section of the design surface of rptMainBM, and click on View Code to display the code view for the report. At the top left of the code view for rptMainBM, click the drop-down arrow and select rptMainBM. At the top right of the code window, click the drop-down arrow and select Detail_Format. This creates an event-handling method for the report's Detail_Format event. Add code to the handler to:
- Create a new DataDynamics OleDBDataSource
- Set the new data source's connection string
- Set the new data source's SQL query
- Set the subreport's data source equal to the new data source
To write the code in C#
- Click in the Detail section of rptMainBM to select the section. Click on the events icon in the Properties window to display available events for the Detail section. Double-click Format. This creates an event-handling method for rptMainBM's Detail_Format event. Add code to the handler to:
- Create a new DataDynamics OleDBDataSource
- Set the new data source's connection string
- Set the new data source's SQL query
- 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 Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
Detail.Format
Dim rpt As New rptSubBM()
Dim subDS As New DataDynamics.ActiveReports.DataSources.OleDBDataSource()
subDS.ConnectionString = Me.ds.ConnectionString
subDS.SQL = "Select * from products where categoryID = " + m_categoryID
rpt.DataSource = subDS
Me.ctlSubreport.Report = rpt
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
rptSub rpt = new rptSubBM();
DataDynamics.ActiveReports.DataSources.OleDBDataSource subDS = new DataDynamics.
ActiveReports.DataSources.OleDBDataSource();
subDS.ConnectionString = this.ds.ConnectionString;
subDS.SQL = "Select * from products where categoryID = " + m_categoryID;
rpt.DataSource = subDS;
ctlSubReport.Report = rpt;
}
Adding code to the Detail_Format event for both reports
To write the code in Visual Basic
- Right-click in any section of the design window of rptMainBM, and click on View Code to display the code view for the report. At the top left of the code view for rptMainBM, click the drop-down arrow and select Detail. At the top right of the code window, click the drop-down arrow and select Format. This creates an event-handling method for rptMainBM's Detail_Format event.
- Right-click in any section of the design window of rptSubBM, and click on View Code to display the code view for the report. At the top left of the code view for rptSubBM, click the drop-down arrow and select Detail. At the top right of the code window, click the drop-down arrow and select Format. This creates an event-handling method for rptSubBM's Detail_Format event.
To write the code in C#
- Click in the Detail section of rptMainBM to select the section. Click on the events icon in the Properties window to display available events for the section. Double-click Format. This creates an event-handling method for rptMainBM's Detail_Format event.
- Click in the Detail section of rptSubBM to select the section. Click on the events icon in the Properties window to display available events for the section. Double-click Format. This creates an event-handling method for rptSubBM's Detail_Format event.
The following example shows what the code for the method looks like for rptMainBM.
' Visual Basic
Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
Detail.Format
Me.Detail.AddBookmark(txtCategoryName.text)
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
Detail.AddBookmark(txtCategoryName.Text);
}
The following example shows what the code for the method looks like for rptSubBM.
' Visual Basic
Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
Detail.Format
Me.Detail.AddBookmark(CType(Me.ParentReport.Sections("Detail").Controls _
("txtCategoryName"), TextBox).Text + "\" + Me.txtProductName.Text)
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
this.Detail.AddBookmark(((TextBox)(this.ParentReport.Sections["Detail"].Controls
["txtCategoryName"])).Text + "\\" + this.txtProductName.Text);
}
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 WinForm Viewer for help.
Viewing the Bookmarks Collection
To view the Bookmarks collection
- Press F5 to run the report.
- Click on the "Table of Contents" icon to view the Bookmarks collection.
Adding Special Bookmarks at Run Time
To create and add special bookmarks to the bookmarks collection at run time, you will need to add the bookmarks to the report document's pages collection since the bookmarks are generated from the pages collection.
' Visual Basic
Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
Detail.Format
Dim i As Integer
Try
For i = 0 To Me.Document.Pages.Count - 1
Me.Document.Pages(1).AddBookmark("New Bookmark", 8)
Next
Catch ex As Exception
End Try
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
for(int i = 0; i<Document.Pages.Count;i++)
{
this.Document.Pages[i].AddBookmark("New Bookmark", 25);
}
}
Note: Only add bookmarks at the Page level during report processing. Do not add or remove them using the BookmarksCollection methods until after the document is completely loaded into the viewer. This is because the viewer clears the BookmarksCollection and then recreates it using the bookmarks that are contained in each individual page.
Samples | Walkthroughs
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.