这篇所实现的是我们的终极目标。
打印所有人员,如果是教师的打印其所有的学生信息。
数据源如下:
正向方法(以学生表作为主表)
由于以学生表作为主表时,如果在从表中未发现该学生数据,是不进行打印的,我们用补全数据的方法来进行打印。
在查询数据时补全数据
把所有的学生也查出来,其他列值为空(使得打印不显示信息)。
报表设计如下(使用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
欢迎任何形式的转载,未经作者同意,请保留此段声明!