C # generate a report using ReportViewer,
This article is a small example of using the Microsoft control ReportViewer for report design.
Knowledge points:
- ReportViewer: Located in the Microsoft. Reporting. WinForms namespace, mainly used for report display
- Report: Reports, files ending with rdlc, and Visual Design of Report templates.
- Report data: built-in fields, parameters, images, and datasets (the main parameters and datasets used in this report)
- ReportParameter: use the name and value to create a new report parameter.
- ReportDataSource: The report data source is associated with the able object.
As follows:
The related code is as follows:
1 /// <summary> 2 /// set report 3 /// </summary> 4 private void SetReport () 5 {6 // Step 1: clear previous data 7 this. rptView. localReport. dataSources. clear (); 8 // Step 2: Specify the report path 9 this. rptView. localReport. reportPath = "Report2.rdlc"; 10 // Step 3: construct a new ableable11 DataTable dt = new DataTable ("DataTable1"); 12 dt. columns. add ("Name"); 13 dt. columns. add ("Score"); 14 dt. columns. add ("Id"); 15 dt. rows. add (new object [] {"", 80, "Y0001"}); 16 dt. rows. add (new object [] {"Mathematics", 75, "S0001"}); 17 dt. rows. add (new object [] {"English", 96, "E0001"}); 18 // The name cannot be entered incorrectly, consistent with the dataset name in the report 19 ReportDataSource rdsItem = new ReportDataSource ("DataSet1", dt); 20 // multiple data sources may exist here 21 this. rptView. localReport. dataSources. add (rdsItem); 22 // Step 4: Construct the parameter 23 List <ReportParameter> lstParameter = new List <ReportParameter> () {24 new ReportParameter ("Title", this.txt Title. text), 25 new ReportParameter ("Id", this.txt Id. text), 26 new ReportParameter ("Name", this.txt Name. text), 27 new ReportParameter ("Age", this.txt Age. text), 28 new ReportParameter ("Sex", this.txt Sex. text), 29 new ReportParameter ("Salary", this.txt Salary. text), 30 new ReportParameter ("Depart", this.txt Depart. text) 31}; 32 this. rptView. localReport. setParameters (lstParameter); 33 this. rptView. zoomMode = ZoomMode. percent; 34 this. rptView. zoomPercent = 100; 35 // Step 5: refresh the report 36 this. rptView. refreshReport (); 37}
View Code
Source code download link