In an application containing the ReportViewer control I have had two following problems :
- When the ReportViewer is rendering, trying to close the Parent Form of the ReportViewer
cause to crash the Application.
- There is no straight way to copy the report data to the Clipboard.
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
RenderingComplete events of the ReportViewer and
FormClosing event of the
To see how the problem is occurred, consider the included source files. After
running the project there are nineteen pages in the report. (32 pages in the print layout
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).
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 an related article by Teo Lachev at
To do this, we should set some attributes at the 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
In the following ComboBox write '= "NoLink:" &
Fields!FieldName.Value' . (Replace FieldName with the name of field)
Using the code
For the rendering problem, a variable named
IsRendering is defined. In
RenderingBegin we set
IsRendering = false. While report is rendering it
prevents closing the form by user. The code is as below :
</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,
IsRendering = false;
For the copy problem, after setting the required attributes at the design time,
we write below code in the
event handler :
<summary></summary>private void reportViewer1_Hyperlink(object sender, HyperlinkEventArgs e)
Uri uri = new Uri(e.Hyperlink);
if (uri.Scheme.ToLower() == "nolink")
e.Cancel = true;