TOC with Hyperlink Functionality in Section Based Report
When designing a report there are many features which might be required to be included in the report. A Table of Content**s** page not only gives a report a more professional look, but also eases navigation for the users viewing the report. In this blog article, I will throw some light on how we can add a TOC page to a report. This will allow users to check what topic is located on which page and will provide them with the ability to directly navigate to the corresponding page using the hyperlink functionality. Let us first see how our TOC page will look like:
In this example we can see the list of Country names displayed on the TOC page. This report provides information about the Companies located in the specified countries. So we have grouped the report on the Country datafield. When we click any country on the TOC page, the current page on the viewer changes to show the page containing information about that country. Having said that, let us now see how we can achieve such a functionality. We are basically creating two reports, one to act as the TOC page and the other to display the report content. Later we merge both the reports together to get a single report. The first thing we need to do is to add bookmarks to the content report on the basis of GroupHeader text:
private void GroupHeader1_Format(object sender, EventArgs e)
{
this.GroupHeader1.AddBookmark(this.TextBox1.Text + "..." + this.PageNumber);
}
Next we will open the TOC report and populate its text to show the Group fields and set the Hyperlink text on it:
public GrapeCity.ActiveReports.Document.Section.BookmarksCollection bm;
private int counter;
private void SectionReport2_DataInitialize(object sender, EventArgs e)
{
this.Fields.Add("bm");
}
private void SectionReport2_FetchData(object sender, FetchEventArgs eArgs)
{
eArgs.EOF = false;
if (counter == this.bm.Count)
{
eArgs.EOF = true;
return;
}
this.Fields["bm"].Value = this.bm[counter].Label;
counter += 1;
}
private void Detail_BeforePrint(object sender, EventArgs e)
{
this.TextBox1.HyperLink = this.TextBox1.Text;
}
The final step involves the merging of the two reports and handling of the hyperlink click on the TOC page. This can be done using the Hyperlink event of the viewer control:
private int bmPageTotal;
private void Form1_Load(object sender, EventArgs e)
{
rptContent rpt = new rptContent();
rpt.Run(false);
rptTOC bmRpt = new rptTOC();
bmRpt.bm = rpt.Document.Bookmarks;
bmRpt.Run(false);
this.bmPageTotal = bmRpt.Document.Pages.Count;
bmRpt.Document.Pages.AddRange(rpt.Document.Pages);
this.viewer1.ReportViewer.RepositionPage = true;
this.viewer1.Document = bmRpt.Document;
}
private void viewer1_HyperLink(object sender, GrapeCity.ActiveReports.Viewer.Win.HyperLinkEventArgs e)
{
string hl = null;
hl = e.HyperLink;
hl = hl.Substring(hl.Length - 1, 1);
int pn = 0;
pn = int.Parse(hl) + this.bmPageTotal;
this.viewer1.ReportViewer.CurrentPage = pn;
}
We are good to go now. Run the project and click on any country on the TOC page. The viewer will automatically display the page containing the information about that country on the viewer. You can download samples in C# and VB.NET which showcase this implementation, using the links provided below. Download C# Sample Download VB.NET Sample