Visual Studio 2017
1.起首建立一個窗體Form,然后添加一個報表瀏覽器ReportViewer,該報表瀏覽器可直接從東西箱中拖拽。如下圖所示:

2.在新建立的Form窗體統一目次下添加報表,右鍵–>添加–>新建項,彈出一個“添加新項”窗口,選擇Reporting菜單項,然后選擇報表,如下圖所示:

3.報表添加完當作后,雙擊打開報表Report1.rdlc,如下圖所示。點擊左側東西箱可以按照需要添加表格,文本框等,設計報表樣式。

4.報表設計好后,需要建立報表所需數據集,同樣右鍵–>添加–>新建項,彈出一個“添加新項”窗口。但此次選擇的是Reporting菜單項上方的“數據”菜單項。然后選擇數據集,輸入數據集名字,點擊確定,完當作數據集建立。如下如所示:

5.雙擊打開新建立的數據集DataSet,空白處右鍵添加數據表,或者從東西箱中拖拽。

6.選中數據表,右鍵添加數據列,并輸入數據列的名字?

7.從頭打開報表Report1.rdlc。在設計好的報表中,點擊報表中單位格右上角的數據庫圖標,添加方才建立的數據集到報表中。

8.在表格設計中可能會涉及到按照類別分組,如下圖所示表格樣式,行按照類別分組?

9.分組方式:選中機型名稱列單位格,右鍵選擇“添加組”–>”行組”–>”父組”,打開Tablix組界面。?

10.添加完當作后,如下圖所示 (列的分組與行分組近似。)

11.有些報表可能需要添加報表參數,按照程序動態改變。?
報表數據中,選擇“參數”,右鍵選擇“添加參數”,如下圖所示?

12.設置參數名稱?

13.參數設置完當作后,東西箱中拖拽一個文本框到報表中,設置文本框名。?

14.然后,右鍵文本框,選擇“文本框”屬性,打開文本框屬性窗口,設置參數名稱和參數值。注重,參數名字必需和文本框名字一致!?

15.填湊數據源 ,在給報表填湊數據源時,習慣于寫一個通用方式。如下是本家兒要代碼片段
///
? ? ? ? /// 生當作圖表
? ? ? ? ///
? ? ? ? /// 數據源
? ? ? ? /// 報表參數
? ? ? ? private void generateChart( DataSet ds_source, ReportParameter[] rp)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (ds_results.Tables.Count > 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? //重置報表
? ? ? ? ? ? ? ? ? ? ? ? this.reportViewer1.Reset();
? ? ? ? ? ? ? ? ? ? ? ? this.reportViewer1.LocalReport.ReportEmbeddedResource = "Report1.rdlc";
? ? ? ? ? ? ? ? ? ? ? ? //指心猿意馬報表參數
? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < rp.Length; i++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp[i] });//與報表有關問題
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //報表數據源
? ? ? ? ? ? ? ? ? ? ReportDataSource rds1 = new ReportDataSource("DataSet1", ds_results.Tables["DataSet1"]);//注重此處數據集名字“DataSet1”必需要和添加的數據集名字不異,不然無法綁定命據源至報表數據集
? ? ? ? ? ? ? ? ? ? ReportDataSource rds2= new ReportDataSource("DataSet2", ds_results.Tables["DataSet2"]);? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? reportViewer1.LocalReport.DataSources.Clear();
? ? ? ? ? ? ? ? ? ? reportViewer1.LocalReport.DataSources.Add(rds1 );
? ? ? ? ? ? ? ? ? ? reportViewer1.LocalReport.DataSources.Add(rds2 );? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? reportViewer1.RefreshReport();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show("沒稀有據!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
? ? ? ? ? ? ? ? ? ? ?//報表數據源
? ? ? ? ? ? ? ? ? ? ReportDataSource rds1 = new ReportDataSource("DataSet1", ds_results.Tables["DataSet1"]);
? ? ? ? ? ? ? ? ? ? ReportDataSource rds2= new ReportDataSource("DataSet2", ds_results.Tables["DataSet2"]);? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? reportViewer1.LocalReport.DataSources.Clear();
? ? ? ? ? ? ? ? ? ? reportViewer1.LocalReport.DataSources.Add(rds1 );
? ? ? ? ? ? ? ? ? ? reportViewer1.LocalReport.DataSources.Add(rds2 );? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? reportViewer1.RefreshReport();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? }
? ? ? ? }
//以下為本家兒方式內代碼:
? ? ? ? ? ? ?#region 綁心猿意馬報表參數
? ? ? ? ? ? ? ? string p1 = "參數1";
? ? ? ? ? ? ? ? string p2="參數2";
? ? ? ? ? ? ? ? ReportParameter[] rp = new ReportParameter[2];//
? ? ? ? ? ? ? ? string[] rptName = new string[2] { "START_END_TIME", "參數名2" };//這里要注重,報表參數和文本框的名字必需一致
? ? ? ? ? ? ? ? object[] rptValue = new object[] { p1, p2};
? ? ? ? ? ? ? ? for (int i = 0; i < rp.Length; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? rp[i] = new ReportParameter(rptName[i], rptValue[i].ToString());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? #endregion
//綁心猿意馬報表參數后,籌辦數據集數據源DataSet,然后挪用上面通用方式
generateChart(ds_source,rp);
以上,即為報表設計和為報表填湊數據的本家兒要步調。
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!