之前從來沒有接觸過報表,突然接到要求做一個報表控件,整個人都是懵的,前前后后花了一周半的時間理清了報表的使用,主要的難點就是報表的分組和參數設置,做個備忘錄以后再用
一.數據源
- 報表的數據源設置有兩處:
一處是在創建報表的時候,使用報表向導,它會先設置數據集屬性,比如新建的數據集名稱和數據源,數據源主要有數據庫,對象,其他應用程序提供的返回數據服務和SharePoint站點(我也不知道是什么)
還有一處是在創建完報表后,打開報表,點擊工具欄的視圖=>報表數據=>數據集(網上很多都說報表數據是在工具欄=>報表=>屬性,但我用的是VS 2012 Ultimate版本和VS 2015 EnterPrise版本,也許路徑不同)
- 數據源我用過兩種,分別是數據庫和對象:
對象很容易理解,我創建了一個用的是包含List表的類文件作為對象,每個表中的JiLiangRDLC元素作為一行數據,表中的元素在綁定報表的數據源之前可以進行處理,比如篩選,運算,
1 public class ListJiLiangRDLC 2 { 3 public List<JiLiangRDLC> rdlcAll { get; set; } 4 public ListJiLiangRDLC() 5 { 6 rdlcAll = new List<JiLiangRDLC>(); 7 } 8 }
數據庫作為對象也很容易理解,添加一個數據集文件,在文件中,添加“工具箱=>數據集=>TableAdapter控件”,在配置向導中配置連接的數據庫表格和需要綁定的列數據,至於如何綁定,會在之后的博客中描述
二.數據分組
在創建報表中的報表向導,會有行組設置和列組設置,一般用的都是行組(行組中分組依據同樣會出現在表格中,所以分組依據中的參數和數據中的參數不要重復),完成報表向導后,會自動生成一張表格,表格的名稱和參數的Name屬性一致,此時分組的結果是這樣的(參考https://www.cnblogs.com/wjbobo/archive/2012/04/13/2446535.html):
我們需要的是這樣的:
那么如何做到呢,很簡單,有兩個辦法:
一個是按照上面網址中的描述,我們先不創建分組依據,在下面行組和列組中,選中行組右鍵添加組.父組->分組依據,添加行組數據,一層層往第一個分組依據加,比如圖片中就是不停的添加父組(分店=>市=>省):
因為我用的報表沒有這么多層,所以我直接選中分組依據以外的數據“合並單元格”,再插入一張表格,將表頭刪除,在插入的表格數據行中逐列填寫數據,此時如果直接運行的話,會報一個"Tablix詳細信息中包含靜態成員"的錯誤,我查了查,解決辦法是在父行組的"詳細信息=>組屬性=>常規”中添加分組方式“=Int((RowNumber(Nothing) - 1)/20)”,原因是在數據行插入了表,而不是在表頭插入了表。
三.報表參數,綁定報表
設置參數,需要在“報表數據=>參數=>右鍵添加參數”,參數一般要允許空白值
接下來就是在代碼中綁定參數數據和綁定報表,刷新報表控件了:
1 public void JiLiangRefresh() 2 { 3 this.reportViewer1.Reset(); 4 this.reportViewer1.LocalReport.Dispose(); 5 this.reportViewer1.LocalReport.DataSources.Clear(); 6 ReportDataSource rds = new ReportDataSource(); 7 rds.Name = "DataSet2"; 8 rds.Value = ListOperator.rdlcAll; 9 ///---向報表綁定數據源 10 this.reportViewer1.LocalReport.DataSources.Insert(0, rds); 11 ///---向報表查看器指定顯示的報表 12 this.reportViewer1.LocalReport.ReportEmbeddedResource = @"ProductSystem.Report.JiLiangReport.rdlc"; 13 ReportParameter repttTxtFixtureBarcode = new ReportParameter("FixtureBarcode", this.deviceId.FixtureBarcode); ; 14 ReportParameter reptTxtDeviceSerialNum = new ReportParameter("DeviceSerialNum", this.deviceId.DeviceSerialNum); 15 ReportParameter reptNumBoxNum = new ReportParameter("BoxNum", this.deviceId.BoxNum); 16 ReportParameter reptTxtDeviceType = new ReportParameter("DeviceType", this.deviceId.DeviceSerial); 17 ReportParameter reptTxtLowerMachineVersion = new ReportParameter("Version", this.deviceId.Version); 18 ReportParameter reptTxtTester = new ReportParameter("Tester", this.deviceId.Tester); 19 ReportParameter reptTxtoddNum = new ReportParameter("oddNum", this.deviceId.oddNum); 20 ReportParameter reptTxtDateTime = new ReportParameter("DateTime", this.deviceId.TestTime.ToString("yyyy-MM-dd")); 21 reportViewer1.LocalReport.SetParameters(new ReportParameter[] { reptTxtoddNum, reptTxtTester, reptTxtLowerMachineVersion, reptTxtDeviceType, 22 reptTxtDateTime, reptTxtDeviceSerialNum, repttTxtFixtureBarcode }); 23 this.reportViewer1.RefreshReport(); 24 //設置打印布局模式,顯示物理頁面大小 25 this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout); 26 //縮放模式為百分比,以100%方式顯示 DateTime.Now.Date; 27 this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent; 28 this.reportViewer1.ZoomPercent = 100; 29 }