VS报表数据源、分组以及参数设置

之前从来没有接触过报表,突然接到要求做一个报表控件,整个人都是懵的,前前后后花了一周半的时间理清了报表的使用,主要的难点就是报表的分组和参数设置,做个备忘录以后再用

一.数据源

  •   报表的数据源设置有两处:

  一处是在创建报表的时候,使用报表向导,它会先设置数据集属性,比如新建的数据集名称和数据源,数据源主要有数据库,对象,其他应用程序提供的返回数据服务和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     }

 

posted @ 2018-11-14 18:59  陈浩楠  阅读(2366)  评论(0编辑  收藏  举报