Search code examples
c#reportviewer

Change behaviour of Print button in ReportViewer C#


I am using a ReportViewer and I would like to print my report.

When I click on the print button, a dialog box opens. It is similar to this.

excel print dialog box

I would like my program to skip this dialog and choose the default printer.

Is there a way to do one of the following things:

  • Just skip the dialog box
  • Map the print button to a function I wrote and that prints the report without showing the dialog box: something similar to myButton.Click += new EventHandler(myButton_Click);

I checked MSDN but found nothing to alter the default toolbar.


I did find this discussion from 2006, where an answer basically says that it is not possible. Hiding the toolbar and creating my own would be a solution but it's a bit overkill.

I am also aware of How to print a ReportViewer's report without showing a form, but that's about being able to choose between showing the form and printing the report. This is not quite my problem because I do want to display the form, and I want that when I click on the Print button, it prints the report, without asking me which printer to use.


Solution

  • I found a solution !

    More precisely I found a workaround that enables you to call a custom printing function.

    You need to use the Print event of the ReportViewer.

    You can then catch the event, cancel it and call your own printing function:

    myViewer.Print += new CancelEventHandler(myViewer_Print);
    void myViewer_Print(object sender, CancelEventArgs e)
    {
        e.Cancel = true;
        MyCustomPrintFunction();
    }