FastReport.NET主从表报表完整版源码
1、DoPrint事件
C# 全选
public override void DoPrint(IButtonInfo button)
{
AssertFocusedRow();
var docNo = gvSummary.GetObject<res_SO>().SONO;
frmReportSO.Execute(docNo, true);
}
2、报表窗体的 Execute事件
C# 全选
public static void Execute(string docNo, bool preview = true)
{
frmReportSO form = new frmReportSO();
form.txtNoFrom.Text = docNo;
form.txtNoTo.Text = docNo;
if (preview)
{
FastReport.Report rptSO = form.InitializeReport();
rptSO.Show();
}
else
{
form.ShowDialog();
}
}
3、InitializeReport 初始化报表核心方法
C# 全选
private FastReport.Report InitializeReport()
{
#region 方法1:加载本地报表文件写法
//打印主从表数据
string file = Application.StartupPath + @"Reports\rptSO.frx";
FastReport.Report rptSO = new FastReport.Report();
rptSO.Load(file);//加载报表模板文件
#endregion
QueryBusinessBase P = new QueryBusinessBase
{
DocNoFrom = txtNoFrom.Text,
DocNoTo = txtNoTo.Text,
DocDateFrom = txtDateFrom.DateTime,
DocDateTo = txtDateTo.DateTime
};
//取报表数据
var data = new bllSO().QueryReportData(P);
rptSO.Tag = data; //缓存报表数据,(放到FastReport.Report.Tag 对象)
//转换成Dataset
var ds = new DataSet();
ds.Tables.Add(DataTableHelper.ToDataTable(data.ListMaster));
ds.Tables.Add(DataTableHelper.ToDataTable(data.ListDetails));
ds.Tables[0].TableName = "M";//换个短的别名
ds.Tables[1].TableName = "D";//换个短的别名
rptSO.RegisterData(ds.Tables[0], "M"); //注册数据源,主表
rptSO.RegisterData(ds.Tables[1], "D"); //注册数据源,从表
(rptSO.GetDataSource("M") as TableDataSource).Enabled = true;
(rptSO.GetDataSource("D") as TableDataSource).Enabled = true;
//给DataBand(主表数据)绑定数据源
DataBand masterBand = rptSO.FindObject("Data1") as DataBand;
masterBand.DataSource = rptSO.GetDataSource("M"); //主表
//给DataBand(明细数据)绑定数据源
DataBand detailBand = rptSO.FindObject("Data2") as DataBand;
detailBand.DataSource = rptSO.GetDataSource("D"); //明细表
//打印图片,需要在BeforePrint事件内处理
detailBand.BeforePrint += new EventHandler(DetailBand_BeforePrint);
//重要!!给明细表设置主外键关系!
detailBand.Relation = new Relation();
detailBand.Relation.ParentColumns = new string[] { "SONO" };
detailBand.Relation.ParentDataSource = rptSO.GetDataSource("M"); //主表
detailBand.Relation.ChildColumns = new string[] { "SONO" };
detailBand.Relation.ChildDataSource = rptSO.GetDataSource("D"); //明细表
//设置自定义参数
rptSO.Parameters.FindByName("pUser").Value = Loginer.CurrentUser.AccountName;
rptSO.Parameters.FindByName("pDate").Value = DateTime.Now;
rptSO.Parameters.FindByName("pReportTitle").Value = CommonData.CompanyInfo.ReportHead;
rptSO.Parameters.FindByName("pAddress").Value = CommonData.CompanyInfo.Address;
rptSO.Parameters.FindByName("pTel").Value = CommonData.CompanyInfo.Tel;
rptSO.Parameters.FindByName("pFax").Value = CommonData.CompanyInfo.Fax;
//获取组件
(rptSO.FindObject("PicLogo") as PictureObject).Image = ImageHelper.FromBytes(CommonData.CompanyInfo.Logo);
return rptSO;
}
4、特殊处理,图片处理
C# 全选
/// <summary>
/// 图片处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DetailBand_BeforePrint(object sender, EventArgs e)
{
//取出当前正在打印的记录(DataRow)。
DataRow row = (sender as DataBand).DataSource.CurrentRow as DataRow;
//获取当前报表缓存数据 (FastReport.Report.Tag 对象)
var source = ((sender as DataBand).Report.Tag as BusinessDataList<res_SO, res_SOs>).ListDetails;
var data = source.Where(w => w.isid == row["isid"].ToStringEx()).FirstOrDefault();
if (data != null && data.ImgSmall != null)
{
Image img = ImageHelper.FromBytes(data.ImgSmall);//将字节转换为图片
PictureObject pic = (sender as DataBand).Report.FindObject("Picture") as PictureObject;
if (pic != null) pic.Image = new Bitmap(img); //注意:必须是Bitmap, Image不受支持!
}
}
5、图片参考
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
CSFrameworkV6 C/S框架网