一、前言 最近做的项目需要单据批量打印的功能,优先想到用RDLC来实现。经过Visual Studio几个版本的发展后,RDLC愈发成熟,操作方式也变得简洁,相比vs2005的版本,有质的提升,不过仍有一下几点缺憾: 1、内置函数不支持C#,只支持Visual Basic 2、不支持Asp.net MVC,支持webForm和winForm 3、VS2008及以下版本开发WebForm时,不建议使用RDLC,因为生成的报表样式不兼容chrome浏览器。 如果未来时间充裕,我将会把RDLC一系列应用实例分享出来。闲话少说,言归正传。 二、测试数据 --订单表 create table fcwOrder ( id int identity(1,1), ordercode varchar(50),--订单号 ordername varchar(50),--订单名字 opername varchar(50),--经手人 comname varchar(50),--客户名称 createtime datetime,--订单日期 barcode image--条形码,预留字段。 ) --订单明细 create table fcwOrderDetails ( id int identity(1,1), ordercode varchar(50),--订单号 procode varchar(50),--产品编号 proname varchar(50),--产品名称 promodel varchar(50),--产品型号 pronum int --订购数量 ) --测试数据 insert into fcwOrder(ordercode,ordername,opername,comname,createtime) values(‘order001‘,‘订单一‘,‘张三一‘,‘客户一‘,‘2014-07-01‘) insert into fcwOrder(ordercode,ordername,opername,comname,createtime) values(‘order002‘,‘订单二‘,‘张三二‘,‘客户二‘,‘2014-07-02‘) insert into fcwOrder(ordercode,ordername,opername,comname,createtime) values(‘order003‘,‘订单三‘,‘张三三‘,‘客户三‘,‘2014-07-03‘) insert into fcwOrderDetails(ordercode,procode,proname,promodel,pronum) values(‘order001‘,‘pro0011‘,‘产品一一‘,‘长1宽1‘,11) insert into fcwOrderDetails(ordercode,procode,proname,promodel,pronum) values(‘order001‘,‘pro0012‘,‘产品一二‘,‘长1宽2‘,12) insert into fcwOrderDetails(ordercode,procode,proname,promodel,pronum) values(‘order002‘,‘pro0021‘,‘产品二一‘,‘长2宽1‘,21) insert into fcwOrderDetails(ordercode,procode,proname,promodel,pronum) values(‘order002‘,‘pro0022‘,‘产品二二‘,‘长2宽2‘,22) insert into fcwOrderDetails(ordercode,procode,proname,promodel,pronum) values(‘order003‘,‘pro0031‘,‘产品三一‘,‘长3宽1‘,31) insert into fcwOrderDetails(ordercode,procode,proname,promodel,pronum) values(‘order003‘,‘pro0032‘,‘产品三二‘,‘长3宽2‘,32) insert into fcwOrderDetails(ordercode,procode,proname,promodel,pronum) values(‘order003‘,‘pro0033‘,‘产品三三‘,‘长3宽3‘,33) --统一订单明细 select o.id as orderid,o.ordercode,o.opername,o.ordername,o.comname,o.createtime,o.barcode, d.id as detailid,d.ordercode detailordercode,d.procode,d.proname,d.promodel,d.pronum from fcwOrder o join fcwOrderDetails d on d.ordercode=o.ordercode 三、编码实现 提示:本案例是属于winform程序,原理上操作RDLC部分后台代码也可用于webform 开发环境:Visual Studio 2012 , Sql server 2012 |
3、【配置数据集】将工具箱中的TableAdapter拖入到数据集设计器中,配置数据库连接后,并将以下语句装载到表中: select o.id as orderid,o.ordercode,o.opername,o.ordername,o.comname,o.createtime,o.barcode, d.id as detailid,d.ordercode detailordercode,d.procode,d.proname,d.promodel,d.pronum from fcwOrder o join fcwOrderDetails d on d.ordercode=o.ordercode 右键单击已经生成的TableAdapter,选择属性,将名称改为Order
|
7【winform代码】.winform 拖入ListBox,Button,ReportViewer等步骤,不再赘述。 ListBox的selectMode设为 MultiSimple, 为ReportView设定RDLC文件 重要代码: private void Form1_Load(object sender, EventArgs e) { //绑定list_order DataTable dtorder = SQLHelper.GetDataTable(@"select o.id as orderid,o.ordercode,o.opername,o.ordername,o.comname,o.createtime,o.barcode from fcwOrder o"); list_order.DataSource = dtorder; list_order.DisplayMember = "ordercode"; list_order.ValueMember = "ordername"; } private void button1_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); //构造多选的order列表 foreach (object obj in list_order.SelectedItems) { DataRowView drv = (DataRowView)obj; if(drv!=null) { sb.Append("‘" + drv.Row["ordercode"].ToString() + "‘,"); } } //为report绑定数据源 if (sb.ToString().Length > 2) { string orderwhere = sb.ToString().Substring(0, sb.ToString().Length - 1); string strsql = @"select o.id as orderid,o.ordercode,o.opername,o.ordername,o.comname,o.createtime,o.barcode, d.id as detailid,d.ordercode detailordercode,d.procode,d.proname,d.promodel,d.pronum from fcwOrder o join fcwOrderDetails d on d.ordercode=o.ordercode where o.ordercode in (" + orderwhere + ")"; DataTable dtorder2 = SQLHelper.GetDataTable(strsql); reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.EnableExternalImages = true; reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Order",dtorder2)); reportViewer1.LocalReport.SetParameters(new ReportParameter("para_Total",dtorder2.Rows.Count.ToString())); reportViewer1.RefreshReport(); } } |