Click here to Skip to main content
15,887,485 members
Articles / Database Development / SQL Server / SQL Server 2008
Tip/Trick

.NET WinForms ReportViewer: Non-printable Report Background (Elements)

Rate me:
Please Sign up or sign in to vote.
4.92/5 (6 votes)
14 Mar 2014CPOL1 min read 17.4K   344   14   2
HowTo: make the report background (elements) of Microsoft .NET Report (.rdlc) non-printable

Introduction

Once I had a task - create a report with a background that will not be printed. After a long and fruitless search for answers on the Internet, I began to solve the problem by myself - and want to share my solution.

Background

The main idea is that .NET ReportViewer uses the same "mechanics" (rendering) to generate report (pre)view and to print report (and, I guess, to generate any exporting file). And where is no property for elements to define when it should be visible. But you can use an expression to set a number of properties - Hidden or BackgroundImage.Source, in my case, using report parameters. So if you know the reason next rendering will be executed for - you can set some parameters to values determining elements visibility, and report will be rendered the way you want.

Using the Code

I will describe only my case - to hide a page background when printing, but you can show|hide report elements in other cases the same way.

First, I added an image and a parameter to my report:

add parameter

add background image

and make a report body background expression like:

VBScript
=IIf(Parameters!HideBg.Value=True, "", "ReportBackground")

set expression

set expression

Second, I added two event handlers to ReportViewer:

event handlers

and in the code:

C#
private void Form1_Load(object sender, EventArgs e)
{
    this.rv.LocalReport.ReportEmbeddedResource = "RerportViewer.NonPrintableBg.Report1.rdlc";
    this.rv.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("HideBg", "False", true));
    this.rv.RefreshReport();
}

private bool printing = false;

private void rv_PrintingBegin(object sender, Microsoft.Reporting.WinForms.ReportPrintEventArgs e)
{
    this.rv.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("HideBg", "True", true));
    this.printing = true;
}

private void rv_RenderingComplete(object sender, Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)
{
    if (this.printing == true)
    {
        this.printing = false;
        this.rv.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("HideBg", "False", true));
    }
}

By setting a parameter before the rendering will be called, you can manage how the report will be rendered. As the ReportViewer has events on every button pressed (and for export too), you can define some variables and set them in event handlers, resetting after rendering is complete to restore the default (viewable) report state.

So, you can see the app:

report in app

and the printed report:

printed to Adobe Acrobat report

Works exactly the way I wanted it to.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionReally Thanks Pin
Aung Paing2-Jun-20 16:41
Aung Paing2-Jun-20 16:41 
QuestionVery interesting article about how to make the report background (elements) of Microsoft .NET Report (.rdlc) non-printable Pin
Volynsky Alex15-Mar-14 3:17
professionalVolynsky Alex15-Mar-14 3: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.