Change behaviour of Print button in ReportViewer C#

偶尔善良 提交于 2019-12-24 04:23:18

问题


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.

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.


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

UPDATE: This is not a duplicate. I know there is a question called "How to print a ReportViewer's report without showing a form", in which the guy wants to be able to choose between showing the form and printing the report. This is not my problem. 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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/30188890/change-behaviour-of-print-button-in-reportviewer-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!