Walkthrough: Bookmarks with Grouping
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 group header Format event
- 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 an ActiveReport to a Visual Studio project
To add an ActiveReport to your project
- Open a new project in Visual Studio.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptGroupBM.
- Click Open.
Connecting the report to a data source
To connect the 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 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 rptGroupBM
- Make the following changes to the group header:
- Change the name to ghCustomers
- Change the DataField property to Country
- Add the following control to the GroupHeader section of rptGroupBM:
Control |
DataField |
Name |
Text/Caption |
Location |
TextBox |
Country |
txtCountry |
Country |
0, 0 |
- Add the following controls to the Detail section of rptGroupBM:
Control |
DataField |
Name |
Text/Caption |
Location |
TextBox |
CompanyName |
txtCompanyName |
Company Name |
0, 0 |
TextBox |
City |
txtCity |
City |
2.375, 0 |
Adding code to the Detail_Format event
To write the code in Visual Basic
- Right-click in any section of the design window of rptGroupBM, and click on View Code to display the code view for the report. At the top left of the code view for rptGroupBM, 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 rptGroupBM's Detail_Format event.
To write the code in C#
- Click in the Detail section of rptGroupBM 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 rptGroupBM's Detail_Format event.
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
Me.Detail.AddBookmark(txtCountry.Text + "\" + txtCity.Text + "\" + _
txtCompanyName.Text)
End Sub
//C#
private void Detail_Format(object sender, System.EventArgs eArgs)
{
Detail.AddBookmark(txtCountry.Text + "\\" + txtCity.Text + "\\" +
txtCompanyName.Text);
}
Adding code to the ghCustomers_Format event
To write the code in Visual Basic
- Right-click in the GroupHeader section of the design window of rptGroupBM, and click on View Code to display the code view for the report. At the top left of the code view for rptGroupBM, click the drop-down arrow and select ghCustomers. At the top right of the code window, click the drop-down arrow and select Format. This creates an event-handling method for rptGroupBM's ghCustomers_Format event.
To write the code in C#
- Click in the GroupHeader section of rptGroupBM 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 rptGroupBM's ghCustomers_Format event.
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(txtCountry.Text)
End Sub
//C#
private void ghCustomers_Format(object sender, System.EventArgs eArgs)
{
this.ghCustomers.AddBookmark(txtCountry.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.