[FastReport]關係(Relation)的使用2

這篇所實現的是我們的終極目標。
打印所有人員,如果是教師的打印其所有的學生信息。
數據源如下:
數據源

正向方法(以學生表作爲主表)

由於以學生表作爲主表時,如果在從表中未發現該學生數據,是不進行打印的,我們用補全數據的方法來進行打印。

在查詢數據時補全數據

把所有的學生也查出來,其他列值爲空(使得打印不顯示信息)。
這裏寫圖片描述
報表設計如下(使用Duplicates屬性對相同值的單元格進行控制):
這裏寫圖片描述
打印預覽如下:
這裏寫圖片描述

在FastReport中通過代碼將數據源補全。

該方法無限制(譬如對應表中不存在學生02,使用上面的方法導致不顯示學生02)。
當然該方法需要一些編程基礎了。
爲Report的StartReport事件添加如下方法(實現如下內容:報表加載時爲兩個數據集添加Load事件委託,數據集數據加載後更新其中的數據。):

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Linq;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;

namespace FastReport
{
  public class ReportScript
  {
    private DataTable _relation = null;
    private DataTable _users = null;

    private void _StartReport(object sender, EventArgs e)
    {
      DataSourceBase relation = Report.GetDataSource("UserRelation");
      DataSourceBase users = Report.GetDataSource("Users");
      relation.Load += ds_Load;
      users.Load += ds_Load;
    }

    private void ds_Load(object sender, EventArgs e)
    {
      if(_relation != null && _users != null) return;

      DataTable dt =(sender as TableDataSource).Table;

      if(dt.TableName == "UserRelation")
      {
        _relation = dt;
      }
      else
      {
        _users = dt;
      }
      if(_relation != null && _users != null)
      {
        List<string> list = _users.AsEnumerable().Select(p => p.Field<string>("ID")).Distinct()
          .Except(_relation.AsEnumerable().Select(p => p.Field<string>("TID2")).Distinct())
          .ToList();
        list.ForEach(p=> _relation.Rows.Add("","","","",p));
        Report.RegisterData(_relation,"UserRelation");
      }
    }
  }
}

代碼中用到了Linq,需要添加System.Linq命名空間;另外需要添加
System.Data.DataSetExtensions.dll和System.Core.dll程序集引用,位於文件夾(C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework)中。
報表設計與1.1中的方法一致,預覽效果也一致。

反向方法(以學生表作爲從表)

思路就是對應表中教師ID是唯一的,將多個學生合併到一起顯示。

關係Relation的查詢語句改造,SQL2005以上版本使用APPLY搞定,這裏就不做介紹了。

在FastReport中通過代碼將數據源進行數據合併。

與1.2中方法類似,依舊爲Report的StartReport事件添加處理方法:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Linq;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;

namespace FastReport
{
  public class ReportScript
  {
    private DataTable _relation = null;

    private void _StartReport(object sender, EventArgs e)
    {
      DataSourceBase relation = Report.GetDataSource("UserRelation");
      relation.Load += ds_Load;
    }

    private void ds_Load(object sender, EventArgs e)
    {
      if(_relation != null) return;

      _relation =(sender as TableDataSource).Table;

      //就取主要的兩列,合併的學生姓名和教師主鍵TID2
      List<Obj> list = _relation.AsEnumerable().GroupBy(p => p.Field<string>("TID2"))
      .Select(g => new Obj
      {
      TID2=g.Key,
      SName = string.Join(",", g.Select(q => q.Field<string>("SName")).ToArray())
      }).ToList();
      _relation.Rows.Clear();
      list.ForEach(p=> _relation.Rows.Add("",p.SName,"","",p.TID2));
      Report.RegisterData(_relation,"UserRelation");
    }
  }

  internal class Obj
  {
    internal string SName;
    internal string TID2;
  }
}

對應關係中把學生表作爲從表,報表設計如下:
這裏寫圖片描述
最後看下效果:
這裏寫圖片描述

是不是完美了,好了,就介紹到這。

作者:FoolRabbit(百度ID:一個人『等待』)
出處:http://blog.csdn.net/rabbitsoft_1987
歡迎任何形式的轉載,未經作者同意,請保留此段聲明!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章