using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using grproLib; namespace InjectionSearch { public class ReportHelper { public GridppReport Report; private DataTable _dtDataDetail; public ReportHelper(string filename, DataTable dtDataHead, DataTable dtDataDetail, int paperOrigntation, short copies, string printerName) { Init(filename, dtDataHead, dtDataDetail, paperOrigntation, copies, printerName); } private void Init(string filename, DataTable dtDataHead, DataTable dtDataDetail, int paperOrigntation, short copies, string printerName) { try { _dtDataDetail = dtDataDetail; Report = new GridppReport(); Report.LoadFromFile(filename); //Report.LoadFromFile("D:\\项目\\北汽模塑一厂\\SVN\\Code\\InjectionPC\\bin\\Debug\\单个条码.grf"); Report.FetchRecord += Report_FetchRecord; Report.Printer.PrinterName = printerName; Report.Printer.PaperOrientation = (GRPaperOrientation)paperOrigntation; Report.Printer.Copies = copies; FillParameters(Report, dtDataHead); } catch (Exception e) { Console.WriteLine(e); throw; } } public void Print(bool isPreview, bool isShowDialog = true) { if (isPreview) Report.PrintPreview(true); else Report.Print(isShowDialog); } private void Report_FetchRecord() { if (_dtDataDetail != null) { FillRecordToReport(Report, _dtDataDetail); } } private void FillRecordToReport(IGridppReport report, DataTable dtDetail) { foreach (DataRow dr in dtDetail.Rows) { report.DetailGrid.Recordset.Append(); foreach (DataColumn dc in dtDetail.Columns) { var field = report.FieldByName(dc.ColumnName); if (field == null) continue; field.Value = dr[dc.ColumnName]; } report.DetailGrid.Recordset.Post(); } } private void FillParameters(GridppReport report, DataTable dtHead) { if (dtHead.Rows.Count == 0) return; foreach (DataColumn dc in dtHead.Columns) { var param = report.ParameterByName(dc.ColumnName); if (param == null) continue; param.Value = dtHead.Rows[0][dc.ColumnName]; } } public static string GetTemplateFile(string sourfilename) { var ofd = new OpenFileDialog { Filter = @"Grid++ files (*.grf)|*.grf", Multiselect = false, RestoreDirectory = true, FileName = sourfilename, InitialDirectory = Path.GetDirectoryName(sourfilename), }; if (ofd.ShowDialog() != DialogResult.OK) return string.Empty; return ofd.FileName; } } }