溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#利用ReportViewer生成報(bào)表

發(fā)布時(shí)間:2020-10-11 07:45:15 來源:腳本之家 閱讀:317 作者:飛翔的月亮 欄目:編程語言

本文主要是利用微軟自帶的控件ReportViewer進(jìn)行報(bào)表設(shè)計(jì)的小例子,具體內(nèi)容如下

涉及知識(shí)點(diǎn):

ReportViewer :位于Microsoft.Reporting.WinForms命名空間, 主要用于報(bào)表的顯示
Report:報(bào)表,以rdlc結(jié)尾的文件,可視化設(shè)計(jì)報(bào)表模板。
報(bào)表數(shù)據(jù):內(nèi)置字段,參數(shù),圖像,數(shù)據(jù)集(本報(bào)表主要使用參數(shù),和數(shù)據(jù)集)
ReportParameter:使用名稱和值實(shí)例化新的報(bào)表參數(shù)
ReportDataSource:報(bào)表的數(shù)據(jù)源與DataTable對(duì)象聯(lián)系起來

效果圖如下:

C#利用ReportViewer生成報(bào)表

相關(guān)代碼如下:

/// <summary>
  /// 設(shè)置報(bào)表
  /// </summary>
  private void SetReport()
  {
   //第一步:清除之前的數(shù)據(jù)
   this.rptView.LocalReport.DataSources.Clear();
   //第二步:指定報(bào)表路徑
   this.rptView.LocalReport.ReportPath = "Report2.rdlc";
   //第三步:構(gòu)造新的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[] { "數(shù)學(xué)", 75, "S0001" });
   dt.Rows.Add(new object[] { "英文", 96, "E0001" });
   //名稱不能寫錯(cuò),和報(bào)表中的數(shù)據(jù)集名稱一致
   ReportDataSource rdsItem = new ReportDataSource("DataSet1", dt);
   //此處可以有多個(gè)數(shù)據(jù)源
   this.rptView.LocalReport.DataSources.Add(rdsItem);
   //第四步:構(gòu)造參數(shù)
   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;
   //第五步:刷新報(bào)表
   this.rptView.RefreshReport();
  }

源碼下載鏈接

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI