Implementation of rdlc

Source: Internet
Author: User

The function in reportviewer is called through reflection to implement the nested function. It is tested and used in winform, and webform is not tested. I wonder if it can be used.

You cannot use reportviewer. setdisplaymode before using a set. After setting, two files will be printed out for unknown reasons.

1 Public   Static   Class Reportviewerext
2 {
3 ///   <Summary>
4 /// Print directly with the default printer.
5 ///   </Summary>
6 ///   <Param name = "reportviewer"> </param>
7 Public   Static   Void Directprint ( This Reportviewer)
8 {
9 Type reportviewertype = Reportviewer. GetType ();
10
11 Fieldinfo printfieldinfo = Reportviewertype. getfield ( " Print " ,
12 Bindingflags. nonpublic | Bindingflags. Instance | Bindingflags. getfield );
13 Canceleventhandler printevent = (Canceleventhandler) printfieldinfo. getvalue (reportviewer );
14 If (Printevent ! =   Null ) {Printevent (reportviewer, Null );}
15
16 Object createemfdeviceinfo = Reportviewertype. invokemember ( " Createemfdeviceinfo " ,
17 Bindingflags. nonpublic | Bindingflags. Instance | Bindingflags. invokemethod, Null ,
18 Reportviewer, New   Object [] { 1 , 1 });
19 Delegate createandregisterstream = Delegate. createdelegate ( Typeof (Createandregisterstream ),
20 Reportviewer, reportviewertype. getmethod ( " Createstreamemfprintonly " ,
21 Bindingflags. nonpublic | Bindingflags. Instance | Bindingflags. invokemethod ));
22 Type internalrenderingcompletedelegatetype =  
23 Reportviewertype. Assembly. GetType ( " Microsoft. Reporting. winforms. internalrenderingcompletedelegate " );
24 Delegate internalrenderingcompletedelegate = Delegate. createdelegate (internalrenderingcompletedelegatetype,
25 Reportviewer, reportviewertype. getmethod ( " Onrenderingcompleteprintonly " ,
26 Bindingflags. nonpublic | Bindingflags. Instance | Bindingflags. invokemethod ));
27 Type postrenderargstype = Reportviewertype. Assembly. GetType ( " Microsoft. Reporting. winforms. postrenderargs " );
28 Object postrenderargs = Activator. createinstance (postrenderargstype, False , True );
29 Object report = Reportviewertype. invokemember ( " Report " , Bindingflags. nonpublic | Bindingflags. getproperty |  
30 Bindingflags. instance, Null , Reportviewer, Null );
31 Object backgroundthread = Reportviewertype. invokemember ( " Backgroundthread " , Bindingflags. nonpublic |  
32 Bindingflags. getproperty | Bindingflags. instance, Null , Reportviewer, Null );
33 Backgroundthread. GetType (). invokemember ( " Beginrender " , Bindingflags. Public | Bindingflags. invokemethod |  
34 Bindingflags. instance, Null , Backgroundthread, New   Object [] { " Image " , True , Createemfdeviceinfo,
35 Createandregisterstream, internalrenderingcompletedelegate, postrenderargs, report });
36
37 Type reportdocumenttype = Reportviewertype. Assembly. GetType ( " Microsoft. Reporting. winforms. reportprintdocument " );
38 Object currentreport = Reportviewertype. invokemember ( " Currentreport " , Bindingflags. nonpublic |  
39 Bindingflags. getproperty | Bindingflags. instance, Null , Reportviewer, Null );
40 Object filemanager = Currentreport. GetType (). invokemember ( " Filemanager " , Bindingflags. getproperty, Null ,
41 Currentreport, Null );
42 Object pagesettings = Reportviewertype. invokemember ( " Pagesettings " , Bindingflags. nonpublic |  
43 Bindingflags. getproperty | Bindingflags. instance, Null , Reportviewer, Null );
44 Object pagesettingsclone = (Icloneable) pagesettings). Clone ();
45 Constructorinfo = Reportdocumenttype. getconstructor (bindingflags. nonpublic |  
46 Bindingflags. instance, Null , New Type [] {filemanager. GetType (), pagesettings. GetType ()}, Null );
47
48 Printdocument document = (Printdocument) constructorinfo. Invoke ( New   Object [] {Filemanager, pagesettingsclone });
49
50 Document. documentname = (String) Report. GetType (). invokemember ( " Displaynameforuse " , Bindingflags. nonpublic |  
51 Bindingflags. getproperty | Bindingflags. instance, Null , Report, Null );
52 Document. printersettings = (Printersettings) reportviewertype. invokemember ( " Createdefaprintprintsettings " ,
53 Bindingflags. nonpublic | Bindingflags. invokemethod | Bindingflags. instance, Null , Reportviewer, Null );
54 Document. printersettings. printrange = Printrange. allpages;
55 Document. Print ();
56 }
57 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.