ActiveReports allows Bookmarks to be easily set up and used with grouping by adding code to the Detail_Format event of the report and in the group header Format event.
This walkthrough illustrates how to set up and use Bookmarks with grouping in a report.
This walkthrough is split up into the following activities:
- Adding an ActiveReport to a Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Adding code to the Detail_Format event
- Adding code to the GroupHeader_Format event
- Previewing the report and Bookmarks collection in the designer
- Viewing the report
- Viewing the Bookmarks collection
- 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 an ActiveReport to a Visual Studio project
To add an ActiveReport to your 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 rptBookmarkGroup.
- Click Open.
Connecting the report to a data source
To connect the report to a data source
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click the Build button.
- 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 customers order by country".
- Click OK to return to the report design surface.
Adding controls to the report to contain data
To add controls to the report
- Add a GroupHeader/Footer section to rptBookmarkGroup by right-clicking on the design surface of the report and selecting Insert > Report Header/Footer.
- Make the following changes to the group header:
- Change the name to ghCustomers
- Change the BackColor property to RosyBrown
- Change the DataField property to Country
- In the Report Explorer, expand the Fields node, then the Bound node. Drag the following field onto the GroupHeader section and set the Text and Location properties of the created textbox as indicated.
Field Text Location Country Country 0, 0 - In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the following properties of each textbox as indicated.
Field Text Location CompanyName Company Name 0, 0 City City 2.375, 0
Adding code to the Detail_Format event
To write the code in Visual Basic
- Double-click in the Detail section of rptBookmarkGroup. This creates an event-handling method for rptBookmarkGroup's Detail_Format event. Add code to the handler to:
- Set up a bookmark for each city
- Nest city bookmarks within each country
To write the code in C#
- Double-click in the Detail section of rptBookmarkGroup. This creates an event-handling method for rptBookmarkGroup's Detail_Format event. Add code to the handler to:
- Set up a bookmark for each city
- Nest city bookmarks within each country
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 Me.Detail1.AddBookmark(txtCountry1.Text + "\" + txtCity1.Text + "\" + _ txtCompanyName1.Text)
End Sub//C# private void detail_Format(object sender, System.EventArgs eArgs) { detail.AddBookmark(txtCountry1.Text + "\\" + txtCity1.Text + "\\" + txtCompanyName1.Text); }
Adding code to the ghCustomers_Format event
To write the code in Visual Basic or C#
- Double-click in the Group Header section of rptBookmarkGroup. This creates an event-handling method for rptBookmarkGroup's ghCustomers_Format event. Add code to the handler to:
- Set up a bookmark for each country
The following example shows what the code for the method looks like.
' Visual Basic Private Sub ghCustomers_Format(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles ghCustomers.Format Me.ghCustomers.AddBookmark(txtCountry1.Text) End Sub
//C# private void ghCustomers_Format(object sender, System.EventArgs eArgs)
{
this.ghCustomers.AddBookmark(txtCountry1.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. 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, 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. |