Home >Backend Development >C#.Net Tutorial >Sample code sharing of how C# uses ReportViewer to generate reports (picture)

Sample code sharing of how C# uses ReportViewer to generate reports (picture)

黄舟
黄舟Original
2017-05-28 10:03:493072browse

This article mainly introduces the code related to generating reports using ReportViewer in detail. It has certain reference value. Interested friends can refer to it.The main content of this article is This is a small example of report design using Microsoft's own

control

ReportViewer. The specific content is as follows

Involved knowledge points:

ReportViewer: Located in Microsoft .Reporting.WinForms namespace, mainly used for report display

Report: report, file ending with rdlc, visual design report template.

Report data: built-in fields, parameters, images,
Datasets
(This report mainly uses parameters, and data sets) ReportParameter: Use names and values ​​to instantiate new report parameters ReportDataSource: The data source of the report is linked to the Data
Table object

The rendering

is as follows:

The relevant codes are as follows:

/// <summary>
  /// 设置报表
  /// </summary>
  private void SetReport()
  {
   //第一步:清除之前的数据
   this.rptView.LocalReport.DataSources.Clear();
   //第二步:指定报表路径
   this.rptView.LocalReport.ReportPath = "Report2.rdlc";
   //第三步:构造新的DataTable
   DataTable dt = new DataTable("DataTable1");
   dt.Columns.Add("Name");
   dt.Columns.Add("Score");
   dt.Columns.Add("Id");
   dt.Rows.Add(new object[] { "语文", 80, "Y0001" });
   dt.Rows.Add(new object[] { "数学", 75, "S0001" });
   dt.Rows.Add(new object[] { "英文", 96, "E0001" });
   //名称不能写错,和报表中的数据集名称一致
   ReportDataSource rdsItem = new ReportDataSource("DataSet1", dt);
   //此处可以有多个数据源
   this.rptView.LocalReport.DataSources.Add(rdsItem);
   //第四步:构造参数
   List<ReportParameter> lstParameter = new List<ReportParameter>() {
    new ReportParameter("Title",this.txtTitle.Text),
    new ReportParameter("Id",this.txtId.Text),
    new ReportParameter("Name",this.txtName.Text),
    new ReportParameter("Age",this.txtAge.Text),
    new ReportParameter("Sex",this.txtSex.Text),
    new ReportParameter("Salary",this.txtSalary.Text),
    new ReportParameter("Depart",this.txtDepart.Text)
   };
   this.rptView.LocalReport.SetParameters(lstParameter);
   this.rptView.ZoomMode = ZoomMode.Percent;
   this.rptView.ZoomPercent = 100;
   //第五步:刷新报表
   this.rptView.RefreshReport();
  }

The above is the detailed content of Sample code sharing of how C# uses ReportViewer to generate reports (picture). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn