C#在Winform開(kāi)發(fā)中使用Grid++報(bào)表
之前一直使用各種報(bào)表工具,如RDLC、DevExpress套件的XtraReport報(bào)表,在之前一些隨筆也有介紹,最近接觸銳浪的Grid++報(bào)表,做了一些測(cè)試?yán)雍洼o助類來(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ò)程。
1、報(bào)表模板設(shè)計(jì)
這個(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)還是蠻好的。
2、數(shù)據(jù)綁定
一般我們綁定數(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)處理DataTable和List<T>等這樣類型數(shù)據(jù)的動(dòng)態(tài)增加操作。
/// <summary> /// 綁定實(shí)體類集合的例子-打印預(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); }
其中輔助類GridReportHelper 代碼如下所示。
/// <summary> /// Gird++報(bào)表的輔助類 /// </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ù)字段名稱與列名稱進(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ù)字段名稱與列名稱進(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); //反射類型 MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, type.GetProperties().Length)]; //根據(jù)字段名稱與列名稱進(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">屬性名稱</param> /// <returns></returns> public static object GetPropertyValue(object obj, string name) { //這個(gè)無(wú)法獲取基類 //PropertyInfo fieldInfo = obj.GetType().GetProperty(name, bf); //return fieldInfo.GetValue(obj, null); //下面方法可以獲取基類屬性 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è)試和輔助類封裝,方便使用。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#項(xiàng)目彩票選號(hào)實(shí)現(xiàn)思路
今天做了一個(gè)彩票選號(hào)的小軟件,將學(xué)到的知識(shí)點(diǎn)總結(jié)一下,下面通過(guò)本文給大家分享C#項(xiàng)目彩票選號(hào)實(shí)現(xiàn)思路,感興趣的朋友跟隨小編一起看看吧2024-08-08Dictionary擴(kuò)展基礎(chǔ)類向字典中添加鍵和值
Dictionary<TKey, TValue> 類是常用的一個(gè)基礎(chǔ)類,但用起來(lái)有時(shí)確不是很方便。本文逐一討論,并使用擴(kuò)展方法解決2013-11-11WPF 在image控件用鼠標(biāo)拖拽出矩形的實(shí)現(xiàn)方法
這篇文章主要介紹了WPF 在image控件用鼠標(biāo)拖拽出矩形的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08使用C#編寫(xiě)簡(jiǎn)單的圖形化的可發(fā)送附件的郵件客戶端程序
這篇文章主要介紹了使用C#編寫(xiě)一個(gè)圖形化的可發(fā)送附件的郵件客戶端程序的方法,文中的示例同樣是基于支持smtp協(xié)議的郵件服務(wù)器,需要的朋友可以參考下2016-02-02C#編寫(xiě)ActiveX網(wǎng)頁(yè)截圖控件
這篇文章主要介紹了C#編寫(xiě)ActiveX網(wǎng)頁(yè)截圖控件,作為學(xué)習(xí)C#編寫(xiě)ActiveX的一個(gè)簡(jiǎn)單入門教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06