雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#在Winform開發(fā)中使用Grid++報表

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

之前一直使用各種報表工具,如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);
      }
    }

以上就是利用這個報表控件做的一些功能測試和輔助類封裝,方便使用。希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:C#機器入門學習之判斷日報是否合格詳解

欄    目:C#教程

下一篇:Unity3D撤回命令開發(fā)詳解

本文標題:C#在Winform開發(fā)中使用Grid++報表

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4830.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有