C#利用ReportViewer生成報(bào)表
本文主要是利用微軟自帶的控件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)系起來
效果圖如下:
相關(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)動(dòng)態(tài)圖標(biāo)閃爍顯示的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)動(dòng)態(tài)圖標(biāo)閃爍顯示的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下2022-12-12學(xué)習(xí)C#靜態(tài)函數(shù)及變量的一個(gè)精典例子與代碼
學(xué)習(xí)C#靜態(tài)函數(shù)及變量的一個(gè)精典例子與代碼...2007-03-03WPF實(shí)現(xiàn)動(dòng)畫效果(一)之基本概念
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫效果之基本概念,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06