[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
欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FoolRabbit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值