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 rptMain to a data source
- Adding controls to rptMain to contain data
- Adding controls to rptSub to contain data
- Adding the code needed to save the current record's CategoryID to use in the rptSub's SQL query
- Adding the code to create a data source for rpt Main and pass it to rptSub
- Adding code to the Detail Format event of both reports to set up 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.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\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.
- 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 rptMain to a data source
To connect the parent report to a data source
- Click on the gray report DataSource icon in the Detail section of rptMain to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click 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 rptMain to contain data
To add controls to the report
- Add the following controls to the Detail section of rptMain:
Control DataField Name Text Location Miscellaneous Label lblProductName Product Name: 0, 0.31 Label lblCategoryName Category Name: 0, 0 TextBox CategoryName txtCategoryName1 CategoryName 1.25, 0 Font = Bold;
ForeColor = DarkGreenSubreport ctlSubreport 1.25, 0.31 - Set the CanShrink property of the Detail section to True.
Adding a control to rptSub to contain data
To add a control to the report
- Set the CanShrink property of the Detail section to True.
- Set the BackColor property of the Detail section to DarkSeaGreen.
- Add the following control to the Detail section of rptSub:
Control DataField Name Text Location Miscellaneous TextBox ProductName txtProductName ProductName 0, 0 Size = 3, 0.19
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 rptMain, and click on View Code to display the code view for the report. At the top left of the code view for rptMain, click the drop-down arrow and select rptMain events. At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for rptMain'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 rptMain 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 rptMain'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 rptMain_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 rptMain_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport3 .FetchEventArgs eArgs) { m_categoryID = Fields["CategoryID"].Value.ToString(); }
Adding the code to create a data source for rptMain and pass it to rptSub
To write the code in Visual Basic or C#
- Double-click in the Detail section of rptMain. This creates an event-handling method for rptMain's Detail_Format event. Add code to the handler to:
- Create a new DataDynamics OleDBDataSource
- Set the connection string
- Set the SQL query
- Pass the new data source to the subreport
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 rptSub() Dim subDS As New DataDynamics.ActiveReports.DataSources.OleDBDataSource() subDS.ConnectionString = Me.DataSource.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 rptSub(); DataDynamics.ActiveReports.DataSources.OleDBDataSource subDS = new DataDynamics. ActiveReports.DataSources.OleDBDataSource(); subDS.ConnectionString = this.DataSource.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 or C#
- Double-click in the Detail section of rptMain to return to the report's Detail Format event.
- Double-click in the Detail section of rptSub. This creates an event-handling method for the report's Detail Format event.
-
The following example shows what the code for the method looks like for rptMain.
' Visual Basic Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format Me.Detail1.AddBookmark(txtCategoryName1.text) End Sub
//C# private void detail_Format(object sender, System.EventArgs eArgs) { detail.AddBookmark(txtCategoryName1.Text); }
The following example shows what the code for the method looks like for rptSub.
' Visual Basic Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format Me.Detail.AddBookmark(CType(Me.ParentReport.Sections("Detail1").Controls _ ("txtCategoryName1"), TextBox).Text + "\" + Me.txtProductName1.Text) End Sub
//C# private void detail_Format(object sender, System.EventArgs eArgs) { this.detail.AddBookmark(((TextBox)(this.ParentReport.Sections["Detail1"].Controls ["txtCategoryName1"])).Text + "\\" + this.txtProductName1.Text); }
Previewing the report and Bookmarks Collection in the designer
To preview the report and Bookmarks Collection in the designer
- Click the "Preview" tab at the bottom of the designer.
- Click on the "Table of Contents" icon to view the Bookmarks collection.
Viewing the report and Bookmarks Collection at run time
To view the report at run time
- Add the ActiveReports viewer control to your Windows form.
- Add the code needed to set the viewer document equal to the report document (rptMain). See Using the ActiveReports Windows Form Viewer for help.
- Press F5 to run the report.
- Click 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 Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.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); } }
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. |