轉(zhuǎn)帖|使用教程|編輯:龔雪|2022-12-09 11:22:00.117|閱讀 248 次
概述:本文主要為大家介紹如何在進(jìn)行WinForm應(yīng)用開(kāi)發(fā)時(shí)使用Grid++報(bào)表,歡迎下載相關(guān)組件體驗(yàn)~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
之前一直使用各種報(bào)表工具,如RDLC、DevExpress 套件的XtraReport報(bào)表,在之前一些文章中也有介紹,最近接觸銳浪的Grid++報(bào)表,做了一些測(cè)試?yán)雍洼o助類(lèi)來(lái)處理報(bào)表內(nèi)容,覺(jué)得還是很不錯(cuò)的,特別是它的作者提供了很多報(bào)表的設(shè)計(jì)模板案例,功能還是非常強(qiáng)大的。試著用來(lái)做一些簡(jiǎn)單的報(bào)表,測(cè)試下功能,發(fā)現(xiàn)常規(guī)的二維表、套打、條形碼二維碼等我關(guān)注的功能都有,是一個(gè)比較強(qiáng)大的報(bào)表控件,本篇隨筆主要介紹在Winform開(kāi)發(fā)中使用Grid++報(bào)表設(shè)計(jì)報(bào)表模板,以及綁定數(shù)據(jù)的處理過(guò)程。
PS:給大家推薦這個(gè)WinForm應(yīng)用界面開(kāi)發(fā)組件——DevExpress WinForms,它能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無(wú)論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
這個(gè)報(bào)表系統(tǒng),報(bào)表模板提供了很多案例,我們可以大概瀏覽下其功能。
它對(duì)應(yīng)在相應(yīng)的文件目錄里面,我們可以逐一查看了解下,感覺(jué)提供這么多報(bào)表還是很贊的,我們可以參考著來(lái)用,非常好。
整個(gè)報(bào)表主要是基于現(xiàn)有數(shù)據(jù)進(jìn)行一個(gè)報(bào)表的模板設(shè)計(jì)的,如果要預(yù)覽效果,我們一般是需要綁定現(xiàn)有的數(shù)據(jù),可以從各種數(shù)據(jù)庫(kù)提供數(shù)據(jù)源,然后設(shè)計(jì)報(bào)表模板,進(jìn)行實(shí)時(shí)的數(shù)據(jù)和格式查看及調(diào)整。
空白的報(bào)表模板大概如下所示,包含頁(yè)眉頁(yè)腳,以及明細(xì)表格的內(nèi)容。
根據(jù)它的教程,模仿著簡(jiǎn)單的做了一個(gè)報(bào)表,也主要是設(shè)計(jì)報(bào)表格式的調(diào)整,和數(shù)據(jù)源的處理的關(guān)系,我們做一個(gè)兩個(gè)報(bào)表就可以很快上手了。
為了動(dòng)態(tài)的加入我們表格所需要的列,我們可以通過(guò)數(shù)據(jù)庫(kù)里面的字段進(jìn)行加入,首先提供數(shù)據(jù)源,指定我們具體的表即可(如果是自定義的信息,則可以手工添加字段)。
這個(gè)里面就是配置不同的數(shù)據(jù)庫(kù)數(shù)據(jù)源了。
如SQLServer數(shù)據(jù)庫(kù)的配置信息如下。
為了方便,我們可以利用案例的Access數(shù)據(jù)庫(kù),也就是Northwind.mdb來(lái)測(cè)試我們的報(bào)表,弄好這些我們指定對(duì)應(yīng)的數(shù)據(jù)表數(shù)據(jù)即可。
這里面配置好數(shù)據(jù)庫(kù)表信息后,我們就可以用它生成相關(guān)的字段和對(duì)應(yīng)的列信息了。
修改列的表頭,讓它符合中文的表頭列,如下所示。
我們?cè)陧?yè)腳出,加入了打印時(shí)間,頁(yè)碼的一些系統(tǒng)變量,具體操作就是添加一個(gè)綜合文本,然后在內(nèi)容里面插入指定的域內(nèi)容即可,如下所示:
預(yù)覽報(bào)表,我們就可以看到具體的報(bào)表格式顯示了。
通過(guò)上面的操作,感覺(jué)生成一個(gè)報(bào)表還是很方便的,接著我有根據(jù)需要做了一個(gè)二維碼的報(bào)表顯示,方便打印資產(chǎn)標(biāo)簽。
綁定數(shù)據(jù)源顯示的報(bào)表視圖如下所示,看起來(lái)還是蠻好的。
一般我們綁定數(shù)據(jù)源,有的時(shí)候可以直接指定數(shù)據(jù)庫(kù)連接,有時(shí)候可以綁定具體的數(shù)據(jù)列表,如DataTable或者List<T>這樣的數(shù)據(jù)源,不同的方式報(bào)表控件的代碼綁定不同。
直接綁定數(shù)據(jù)表的路徑如下所示。
/// <summary> /// 普通連接數(shù)據(jù)庫(kù)的例子-打印預(yù)覽 /// </summary> private void btnNormalDatabase_Click(object sender, EventArgs e) { Report = new GridppReport(); string reportPath = Path.Combine(Application.StartupPath, "Reports\\testgrid++.grf"); string dbPath = Path.Combine(Application.StartupPath, "Data\\NorthWind.mdb"); //從對(duì)應(yīng)文件中載入報(bào)表模板數(shù)據(jù) Report.LoadFromFile(reportPath); //設(shè)置與數(shù)據(jù)源的連接串,因?yàn)樵谠O(shè)計(jì)時(shí)指定的數(shù)據(jù)庫(kù)路徑是絕對(duì)路徑。 if (Report.DetailGrid != null) { string connstr = Utility.GetDatabaseConnectionString(dbPath); Report.DetailGrid.Recordset.ConnectionString = connstr; } Report.PrintPreview(true); }
而如果需要綁定和數(shù)據(jù)庫(kù)無(wú)關(guān)的動(dòng)態(tài)數(shù)據(jù)源,那么就需要通過(guò)控件的FetchRecord進(jìn)行處理了,如下代碼所示。
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
通過(guò)這樣我們?cè)黾用恳粋€(gè)對(duì)應(yīng)的列單元格信息,如下是隨帶案例所示:
//在C#中一次填入一條記錄不能成功,只能使用一次將記錄全部填充完的方式 private void ReportFetchRecord() { //將全部記錄一次填入 Report.DetailGrid.Recordset.Append(); FillRecord1(); Report.DetailGrid.Recordset.Post(); Report.DetailGrid.Recordset.Append(); FillRecord2(); Report.DetailGrid.Recordset.Post(); Report.DetailGrid.Recordset.Append(); FillRecord3(); Report.DetailGrid.Recordset.Post(); } private void FillRecord1() { C1Field.AsString = "A"; I1Field.AsInteger = 1; F1Field.AsFloat = 1.01; } private void FillRecord2() { C1Field.AsString = "B"; I1Field.AsInteger = 2; F1Field.AsFloat = 1.02; } private void FillRecord3() { C1Field.AsString = "C"; I1Field.AsInteger = 3; F1Field.AsFloat = 1.03; }
這樣處理肯定很麻煩,我們常規(guī)做法是弄一個(gè)輔助類(lèi),來(lái)處理DataTable和List<T>等這樣類(lèi)型數(shù)據(jù)的動(dòng)態(tài)增加操作。
/// <summary> /// 綁定實(shí)體類(lèi)集合的例子-打印預(yù)覽 /// </summary> private void btnBindList_Click(object sender, EventArgs e) { Report = new GridppReport(); //從對(duì)應(yīng)文件中載入報(bào)表模板數(shù)據(jù) string reportPath = Path.Combine(Application.StartupPath, "Reports\\testList.grf"); Report.LoadFromFile(reportPath); Report.FetchRecord += ReportList_FetchRecord; Report.PrintPreview(true); } /// <summary> /// 綁定DataTable的例子-打印預(yù)覽 /// </summary> private void btnBindDatatable_Click(object sender, EventArgs e) { Report = new GridppReport(); //從對(duì)應(yīng)文件中載入報(bào)表模板數(shù)據(jù) string reportPath = Path.Combine(Application.StartupPath, "Reports\\testList.grf"); Report.LoadFromFile(reportPath); Report.FetchRecord += ReportList_FetchRecord2; Report.PrintPreview(true); } private void ReportList_FetchRecord() { List<ProductInfo> list = BLLFactory<Product>.Instance.GetAll(); GridReportHelper.FillRecordToReport<ProductInfo>(Report, list); } private void ReportList_FetchRecord2() { var dataTable = BLLFactory<Product>.Instance.GetAllToDataTable(); GridReportHelper.FillRecordToReport(Report, dataTable); }
其中輔助類(lèi) GridReportHelper 代碼如下所示。
/// <summary> /// Gird++報(bào)表的輔助類(lèi) /// </summary> public class GridReportHelper { private struct MatchFieldPairType { public IGRField grField; public int MatchColumnIndex; } /// <summary> /// 將 DataReader 的數(shù)據(jù)轉(zhuǎn)儲(chǔ)到 Grid++Report 的數(shù)據(jù)集中 /// </summary> /// <param name="Report">報(bào)表對(duì)象</param> /// <param name="dr">DataReader對(duì)象</param> public static void FillRecordToReport(IGridppReport Report, IDataReader dr) { MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, dr.FieldCount)]; //根據(jù)字段名稱(chēng)與列名稱(chēng)進(jìn)行匹配,建立DataReader字段與Grid++Report記錄集的字段之間的對(duì)應(yīng)關(guān)系 int MatchFieldCount = 0; for (int i = 0; i < dr.FieldCount; ++i) { foreach (IGRField fld in Report.DetailGrid.Recordset.Fields) { if (string.Compare(fld.RunningDBField, dr.GetName(i), true) == 0) { MatchFieldPairs[MatchFieldCount].grField = fld; MatchFieldPairs[MatchFieldCount].MatchColumnIndex = i; ++MatchFieldCount; break; } } } // 將 DataReader 中的每一條記錄轉(zhuǎn)儲(chǔ)到Grid++Report 的數(shù)據(jù)集中去 while (dr.Read()) { Report.DetailGrid.Recordset.Append(); for (int i = 0; i < MatchFieldCount; ++i) { var columnIndex = MatchFieldPairs[i].MatchColumnIndex; if (!dr.IsDBNull(columnIndex)) { MatchFieldPairs[i].grField.Value = dr.GetValue(columnIndex); } } Report.DetailGrid.Recordset.Post(); } } /// <summary> /// 將 DataTable 的數(shù)據(jù)轉(zhuǎn)儲(chǔ)到 Grid++Report 的數(shù)據(jù)集中 /// </summary> /// <param name="Report">報(bào)表對(duì)象</param> /// <param name="dt">DataTable對(duì)象</param> public static void FillRecordToReport(IGridppReport Report, DataTable dt) { MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, dt.Columns.Count)]; //根據(jù)字段名稱(chēng)與列名稱(chēng)進(jìn)行匹配,建立DataReader字段與Grid++Report記錄集的字段之間的對(duì)應(yīng)關(guān)系 int MatchFieldCount = 0; for (int i = 0; i < dt.Columns.Count; ++i) { foreach (IGRField fld in Report.DetailGrid.Recordset.Fields) { if (string.Compare(fld.Name, dt.Columns[i].ColumnName, true) == 0) { MatchFieldPairs[MatchFieldCount].grField = fld; MatchFieldPairs[MatchFieldCount].MatchColumnIndex = i; ++MatchFieldCount; break; } } } // 將 DataTable 中的每一條記錄轉(zhuǎn)儲(chǔ)到 Grid++Report 的數(shù)據(jù)集中去 foreach (DataRow dr in dt.Rows) { Report.DetailGrid.Recordset.Append(); for (int i = 0; i < MatchFieldCount; ++i) { var columnIndex = MatchFieldPairs[i].MatchColumnIndex; if (!dr.IsNull(columnIndex)) { MatchFieldPairs[i].grField.Value = dr[columnIndex]; } } Report.DetailGrid.Recordset.Post(); } } /// <summary> /// List加載數(shù)據(jù)集 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Report">報(bào)表對(duì)象</param> /// <param name="list">列表數(shù)據(jù)</param> public static void FillRecordToReport<T>(IGridppReport Report, List<T> list) { Type type = typeof(T); //反射類(lèi)型 MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, type.GetProperties().Length)]; //根據(jù)字段名稱(chēng)與列名稱(chēng)進(jìn)行匹配,建立字段與Grid++Report記錄集的字段之間的對(duì)應(yīng)關(guān)系 int MatchFieldCount = 0; int i = 0; MemberInfo[] members = type.GetMembers(); foreach (MemberInfo memberInfo in members) { foreach (IGRField fld in Report.DetailGrid.Recordset.Fields) { if (string.Compare(fld.Name, memberInfo.Name, true) == 0) { MatchFieldPairs[MatchFieldCount].grField = fld; MatchFieldPairs[MatchFieldCount].MatchColumnIndex = i; ++MatchFieldCount; break; } } ++i; } // 將 DataTable 中的每一條記錄轉(zhuǎn)儲(chǔ)到 Grid++Report 的數(shù)據(jù)集中去 foreach (T t in list) { Report.DetailGrid.Recordset.Append(); for (i = 0; i < MatchFieldCount; ++i) { object objValue = GetPropertyValue(t, MatchFieldPairs[i].grField.Name); if (objValue != null) { MatchFieldPairs[i].grField.Value = objValue; } } Report.DetailGrid.Recordset.Post(); } } /// <summary> /// 獲取對(duì)象實(shí)例的屬性值 /// </summary> /// <param name="obj">對(duì)象實(shí)例</param> /// <param name="name">屬性名稱(chēng)</param> /// <returns></returns> public static object GetPropertyValue(object obj, string name) { //這個(gè)無(wú)法獲取基類(lèi) //PropertyInfo fieldInfo = obj.GetType().GetProperty(name, bf); //return fieldInfo.GetValue(obj, null); //下面方法可以獲取基類(lèi)屬性 object result = null; foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(obj)) { if (prop.Name == name) { result = prop.GetValue(obj); } } return result; } }
綁定數(shù)據(jù)的報(bào)表效果如下所示 :
導(dǎo)出報(bào)表為PDF也是比較常規(guī)的操作,這個(gè)報(bào)表控件也可以實(shí)現(xiàn)PDF等格式文件的導(dǎo)出,如下所示。
private void btnExportPdf_Click(object sender, EventArgs e) { List<ProductInfo> list = BLLFactory<Product>.Instance.GetAll(); //從對(duì)應(yīng)文件中載入報(bào)表模板數(shù)據(jù) string reportPath = Path.Combine(Application.StartupPath, "Reports\\testList.grf"); GridExportHelper helper = new GridExportHelper(reportPath); string fileName = "d:\\my.pdf"; var succeeded = helper.ExportPdf(list, fileName); if(succeeded) { Process.Start(fileName); } }
以上就是利用這個(gè)報(bào)表控件做的一些功能測(cè)試和輔助類(lèi)封裝,方便使用。
本文轉(zhuǎn)載自:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: