C# 利用ReportViewer產生報表

來源:互聯網
上載者:User

標籤:pre   oom   forms   img   知識   microsoft   close   inf   c#   

本文主要是利用微軟內建的控制項ReportViewer進行報表設計的小例子

涉及知識點:

  • ReportViewer :位於Microsoft.Reporting.WinForms命名空間, 主要用於報表的顯示
  • Report:報表,以rdlc結尾的檔案,可視化設計報表範本。
  • 報表資料:內建欄位,參數,映像,資料集(本報表主要使用參數,和資料集)
  • ReportParameter:使用名稱和值執行個體化新的報表參數
  • ReportDataSource:報表的資料來源與DataTable對象聯絡起來

如下:

相關代碼如下:

 1 /// <summary> 2         /// 設定報表 3         /// </summary> 4         private void SetReport() 5         { 6             //第一步:清除之前的資料 7             this.rptView.LocalReport.DataSources.Clear(); 8             //第二步:指定報表路徑 9             this.rptView.LocalReport.ReportPath = "Report2.rdlc";10             //第三步:構造新的DataTable11             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[] { "數學", 75, "S0001" });17             dt.Rows.Add(new object[] { "英文", 96, "E0001" });18             //名稱不能寫錯,和報表中的資料集名稱一致19             ReportDataSource rdsItem = new ReportDataSource("DataSet1", dt);20             //此處可以有多個資料來源21             this.rptView.LocalReport.DataSources.Add(rdsItem);22             //第四步:構造參數23             List<ReportParameter> lstParameter = new List<ReportParameter>() {24                 new ReportParameter("Title",this.txtTitle.Text),25                 new ReportParameter("Id",this.txtId.Text),26                 new ReportParameter("Name",this.txtName.Text),27                 new ReportParameter("Age",this.txtAge.Text),28                 new ReportParameter("Sex",this.txtSex.Text),29                 new ReportParameter("Salary",this.txtSalary.Text),30                 new ReportParameter("Depart",this.txtDepart.Text)31             };32             this.rptView.LocalReport.SetParameters(lstParameter);33             this.rptView.ZoomMode = ZoomMode.Percent;34             this.rptView.ZoomPercent = 100;35             //第五步:重新整理報表36             this.rptView.RefreshReport();37         }
View Code

源碼下載連結

C# 利用ReportViewer產生報表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.