using System;
using System.Collections.Generic;
using System.Text;
using grproLib;
using System.Data;
using Stone.Common;
namespace Stone.Common
{
public class MyReport
{
public GridppReport Report = null;
public DataTable dtDataHead = null;
public DataTable dtDataDetail = null;
public MyReport(string filename, DataTable _dtDataHead, DataTable _dtDataDetail)
{
dtDataHead = _dtDataHead;
dtDataDetail = _dtDataDetail;
Report = new GridppReport();
Report.LoadFromFile(filename);
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(Report_FetchRecord);
Report.Printer.PrinterName = MyAppconfig.ReadValue("默认打印机");
FillParameters(); //添加参数
}
///
/// 打印报表
///
public void Print()
{
Report.Print(true);
}
///
/// 打印预览
///
public void PrintPreview()
{
Report.PrintPreview(true);
}
public void Report_FetchRecord()
{
if (dtDataDetail != null)
{
FillRecordToReport(Report, dtDataDetail);
}
}
private struct MatchFieldPairType
{
public IGRField grField;
public int MatchColumnIndex;
}
// 将 DataTable 的数据转储到 Grid++Report 的数据集中
public void FillRecordToReport(IGridppReport _Report, DataTable dt)
{
MatchFieldPairType[] MatchFieldPairs = new MatchFieldPairType[Math.Min(_Report.DetailGrid.Recordset.Fields.Count, dt.Columns.Count)];
//根据字段名称与列名称进行匹配,建立DataReader字段与Grid++Report记录集的字段之间的对应关系
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 中的每一条记录转储到 Grid++Report 的数据集中去
foreach (DataRow dr in dt.Rows)
{
_Report.DetailGrid.Recordset.Append();
for (int i = 0; i < MatchFieldCount; ++i)
{
if (!dr.IsNull(MatchFieldPairs[i].MatchColumnIndex))
MatchFieldPairs[i].grField.Value = dr[MatchFieldPairs[i].MatchColumnIndex];
}
_Report.DetailGrid.Recordset.Post();
}
}
//填充参数
public void FillParameters()
{
if (dtDataHead.Rows.Count == 0) return;
for (int i = 0; i < dtDataHead.Columns.Count; ++i)
{
foreach (IGRParameter fpar in Report.Parameters)
{
if (String.Compare(fpar.Name, dtDataHead.Columns[i].ColumnName, true) == 0)
{
fpar.Value = dtDataHead.Rows[0][dtDataHead.Columns[i].ColumnName];
break;
}
}
}
}
}
}