C#在Winform開發(fā)中使用Grid++報表
之前一直使用各種報表工具,如RDLC、DevExpress套件的XtraReport報表,在之前一些隨筆也有介紹,最近接觸銳浪的Grid++報表,做了一些測試例子和輔助類來處理報表內(nèi)容,覺得還是很不錯的,特別是它的作者提供了很多報表的設計模板案例,功能還是非常強大的。試著用來做一些簡單的報表,測試下功能,發(fā)現(xiàn)常規(guī)的二維表、套打、條形碼二維碼等我關(guān)注的功能都有,是一個比較強大的報表控件,本篇隨筆主要介紹在Winform開發(fā)中使用Grid++報表設計報表模板,以及綁定數(shù)據(jù)的處理過程。
1、報表模板設計
這個報表系統(tǒng),報表模板提供了很多案例,我們可以大概瀏覽下其功能。
它對應在相應的文件目錄里面,我們可以逐一查看了解下,感覺提供這么多報表還是很贊的,我們可以參考著來用,非常好。
整個報表主要是基于現(xiàn)有數(shù)據(jù)進行一個報表的模板設計的,如果要預覽效果,我們一般是需要綁定現(xiàn)有的數(shù)據(jù),可以從各種數(shù)據(jù)庫提供數(shù)據(jù)源,然后設計報表模板,進行實時的數(shù)據(jù)和格式查看及調(diào)整。
空白的報表模板大概如下所示,包含頁眉頁腳,以及明細表格的內(nèi)容。
根據(jù)它的教程,模仿著簡單的做了一個報表,也主要是設計報表格式的調(diào)整,和數(shù)據(jù)源的處理的關(guān)系,我們做一個兩個報表就可以很快上手了。
為了動態(tài)的加入我們表格所需要的列,我們可以通過數(shù)據(jù)庫里面的字段進行加入,首先提供數(shù)據(jù)源,指定我們具體的表即可(如果是自定義的信息,則可以手工添加字段)
這個里面就是配置不同的數(shù)據(jù)庫數(shù)據(jù)源了
如SQLServer數(shù)據(jù)庫的配置信息如下。
為了方便,我們可以利用案例的Access數(shù)據(jù)庫,也就是Northwind.mdb來測試我們的報表,弄好這些我們指定對應的數(shù)據(jù)表數(shù)據(jù)即可。
這里面配置好數(shù)據(jù)庫表信息后,我們就可以用它生成相關(guān)的字段和對應的列信息了
修改列的表頭,讓它符合中文的表頭列,如下所示。
我們在頁腳出,加入了打印時間,頁碼的一些系統(tǒng)變量,具體操作就是添加一個綜合文本,然后在內(nèi)容里面插入指定的域內(nèi)容即可,如下所示
預覽報表,我們就可以看到具體的報表格式顯示了。
通過上面的操作,感覺生成一個報表還是很方便的,接著我有根據(jù)需要做了一個二維碼的報表顯示,方便打印資產(chǎn)標簽。
綁定數(shù)據(jù)源顯示的報表視圖如下所示,看起來還是蠻好的。
2、數(shù)據(jù)綁定
一般我們綁定數(shù)據(jù)源,有的時候可以直接指定數(shù)據(jù)庫連接,有時候可以綁定具體的數(shù)據(jù)列表,如DataTable或者List<T>這樣的數(shù)據(jù)源,不同的方式報表控件的代碼綁定不同。
直接綁定數(shù)據(jù)表的路徑如下所示。
/// <summary>
/// 普通連接數(shù)據(jù)庫的例子-打印預覽
/// </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");
//從對應文件中載入報表模板數(shù)據(jù)
Report.LoadFromFile(reportPath);
//設置與數(shù)據(jù)源的連接串,因為在設計時指定的數(shù)據(jù)庫路徑是絕對路徑。
if (Report.DetailGrid != null)
{
string connstr = Utility.GetDatabaseConnectionString(dbPath);
Report.DetailGrid.Recordset.ConnectionString = connstr;
}
Report.PrintPreview(true);
}
而如果需要綁定和數(shù)據(jù)庫無關(guān)的動態(tài)數(shù)據(jù)源,那么就需要通過控件的FetchRecord進行處理了,如下代碼所示。
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
通過這樣我們增加每一個對應的列單元格信息,如下是隨帶案例所示
//在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ī)做法是弄一個輔助類,來處理DataTable和List<T>等這樣類型數(shù)據(jù)的動態(tài)增加操作。
/// <summary>
/// 綁定實體類集合的例子-打印預覽
/// </summary>
private void btnBindList_Click(object sender, EventArgs e)
{
Report = new GridppReport();
//從對應文件中載入報表模板數(shù)據(jù)
string reportPath = Path.Combine(Application.StartupPath, "Reports\\testList.grf");
Report.LoadFromFile(reportPath);
Report.FetchRecord += ReportList_FetchRecord;
Report.PrintPreview(true);
}
/// <summary>
/// 綁定DataTable的例子-打印預覽
/// </summary>
private void btnBindDatatable_Click(object sender, EventArgs e)
{
Report = new GridppReport();
//從對應文件中載入報表模板數(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++報表的輔助類
/// </summary>
public class GridReportHelper
{
private struct MatchFieldPairType
{
public IGRField grField;
public int MatchColumnIndex;
}
/// <summary>
/// 將 DataReader 的數(shù)據(jù)轉(zhuǎn)儲到 Grid++Report 的數(shù)據(jù)集中
/// </summary>
/// <param name="Report">報表對象</param>
/// <param name="dr">DataReader對象</param>
public static void FillRecordToReport(IGridppReport Report, IDataReader dr)
{
MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, dr.FieldCount)];
//根據(jù)字段名稱與列名稱進行匹配,建立DataReader字段與Grid++Report記錄集的字段之間的對應關(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)儲到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)儲到 Grid++Report 的數(shù)據(jù)集中
/// </summary>
/// <param name="Report">報表對象</param>
/// <param name="dt">DataTable對象</param>
public static void FillRecordToReport(IGridppReport Report, DataTable dt)
{
MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(Report.DetailGrid.Recordset.Fields.Count, dt.Columns.Count)];
//根據(jù)字段名稱與列名稱進行匹配,建立DataReader字段與Grid++Report記錄集的字段之間的對應關(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)儲到 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">報表對象</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ù)字段名稱與列名稱進行匹配,建立字段與Grid++Report記錄集的字段之間的對應關(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)儲到 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>
/// 獲取對象實例的屬性值
/// </summary>
/// <param name="obj">對象實例</param>
/// <param name="name">屬性名稱</param>
/// <returns></returns>
public static object GetPropertyValue(object obj, string name)
{
//這個無法獲取基類
//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ù)的報表效果如下所示
導出報表為PDF也是比較常規(guī)的操作,這個報表控件也可以實現(xiàn)PDF等格式文件的導出,如下所示。
private void btnExportPdf_Click(object sender, EventArgs e)
{
List<ProductInfo> list = BLLFactory<Product>.Instance.GetAll();
//從對應文件中載入報表模板數(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);
}
}
以上就是利用這個報表控件做的一些功能測試和輔助類封裝,方便使用。希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10WinForm實現(xiàn)自定義右下角提示效果的方法
- 01-10.net2.0+ Winform項目實現(xiàn)彈出容器層
- 01-10winform 實現(xiàn)控制輸入法
- 01-10C#實現(xiàn)將窗體固定在顯示器的左上角且不能移動的方法
- 01-10WinForm實現(xiàn)程序一段時間不運行自動關(guān)閉的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法


