| 一、前言   最近做的項目需要單據批量打印的功能,優先想到用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(); } } |