Click here to Skip to main content
15,878,809 members
Articles / Programming Languages / C#

Two Simple Workarounds for the ReportViewer Problems

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
4 Jan 20072 min read 89.9K   1.7K   31   11
An article on the ReportViewer problems

Introduction

In an application containing the ReportViewer control, I have had the following two problems:

  1. When the ReportViewer is rendering, trying to close the Parent Form of the ReportViewer may cause the application to crash.
  2. There is no straight way to copy the report data to the Clipboard.

Description

Rendering Problem

If there are many pages in a report, it takes a few seconds to render. In this case, if the user wants to cancel report rendering and closes the Form containing the ReportViewer control, the application will crash. To solve this problem, I used the RenderingBegin and RenderingComplete events of the ReportViewer and the FormClosing event of the Parent Form.

To see how the problem occurred, consider the included source files. After running the project, there are nineteen pages in the report. (32 pages in the print layout mode). By clicking the 'Print Layout' button, the report begins rendering. While the report is rendering, if the user closes the Parent Form, the application will crash (if we comment added event handlers).

Image 1

Image 2

Copy to the Clipboard

In a report, there is no straight way to copy the report data to the Clipboard. The only way that is available is using the Hyperlink event of the ReportViewer control (There is a related article by Teo Lachev at this link). To do this, we should set some attributes at design time:

  • Suppose that there is a 'table' item on the report file. Select each TextBox from the 'Table Details' row.

  • Go to the Properties -> Navigation -> 'Hyperlink action' -> 'Jump to URL'.

  • In the following ComboBox, write '= "NoLink:" & Fields!FieldName.Value'. (Replace FieldName with the name of field.)

Image 3

Using the Code

For the rendering problem, a variable named IsRendering is defined. In the RenderingBegin, we set IsRendering = false. While report is rendering, it prevents closing the form by user. The code is as below:

C#
<summary></summary>
bool IsRendering = false;

<summary>// It cancels the FormClosing while the ReportViewer is rendering.</summary>
private void ReportForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (IsRendering) e.Cancel = true;
}

<summary>// When rendering begins it sets IsRendering = true.</summary>
private void reportViewer1_RenderingBegin(object sender, CancelEventArgs e)
{
    IsRendering = true;
}

<summary>// When rendering is completed, it sets IsRendering = false.</summary>
private void reportViewer1_RenderingComplete(object sender,
                                        RenderingCompleteEventArgs e)
{
    IsRendering = false;
}

For the copy problem, after setting the required attributes at design time, we write the below code in the reportViewer1_Hyperlink event handler:

C#
<summary>// Copy the report data to the Clipboard.</summary>
private void reportViewer1_Hyperlink(object sender, HyperlinkEventArgs e)
{
    Uri uri = new Uri(e.Hyperlink);
    if (uri.Scheme.ToLower() == "nolink")
    {
        e.Cancel = true;
        Clipboard.Clear();
        Clipboard.SetText(uri.AbsolutePath);
    }
}

History

  • 3rd January, 2007: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe ReportViewer Toolbar Pin
Kschuler25-Jan-07 8:30
Kschuler25-Jan-07 8:30 
AnswerRe: The ReportViewer Toolbar Pin
Afrasiab Cheraghi26-Jan-07 0:28
Afrasiab Cheraghi26-Jan-07 0:28 
QuestionRe: The ReportViewer Toolbar Pin
Kschuler29-Jan-07 8:35
Kschuler29-Jan-07 8:35 
AnswerRe: The ReportViewer Toolbar Pin
Vantigate19-Jul-07 6:17
Vantigate19-Jul-07 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.