using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Drawing.Printing;
|
using System.Text;
|
using System.Windows.Forms;
|
|
namespace GSBase
|
{
|
public partial class MyFastReportMain : Form
|
{
|
public MyFastReportMain()
|
{
|
InitializeComponent();
|
}
|
|
private DataSet Tables = null;
|
public FastReport.Report MyReport = new FastReport.Report();
|
private PrinterSettings printerSettings=null ;
|
private PrintType PrintType;
|
|
/// <summary>
|
/// 外部调用(预览模式和打印模式)
|
/// </summary>
|
/// <param name="_printType"></param>
|
/// <param name="_ds"></param>
|
/// <param name="_reportstring">报表文件模板</param>
|
public void Print(PrintType _printType, DataSet _ds, string _reportstring)
|
{
|
Tables = _ds;
|
PrintType = _printType;
|
DoPrint(_reportstring);
|
}
|
|
public void PrintSettings(PrinterSettings A)
|
{
|
printerSettings = A;
|
|
}
|
|
/// <summary>
|
/// 内部调用
|
/// </summary>
|
/// <param name="reportstring"></param>
|
private void DoPrint(string reportstring)
|
{
|
MyReport.LoadFromString(reportstring);
|
|
DataSet ds;
|
if (Tables == null || Tables.Tables.Count == 0)
|
{
|
ds = new DataSet("MyTables");
|
DataTable dt = new DataTable("MyTableOnew");
|
dt.Columns.Add("name");
|
dt.Columns.Add("age");
|
dt.Columns.Add("sex");
|
for (int i = 1; i <= 100; i++)
|
{
|
DataRow dr = dt.NewRow();
|
dr["name"] = "小明" + i.ToString();
|
dr["age"] = i.ToString();
|
dr["sex"] = "男";
|
dt.Rows.Add(dr);
|
}
|
ds.Tables.Add(dt);
|
}
|
else
|
{
|
ds = Tables;
|
}
|
MyReport.RegisterData(ds);
|
|
MyReport.Preview = this.previewControl1;
|
switch (PrintType)
|
{
|
case PrintType.DesignReport:
|
MyReport.Design();
|
break;
|
case PrintType.PreviewReport:
|
printerSettings = null;//预览时清除打印机,让用户有选择打印机的机会
|
MyReport.Show();
|
this.ShowDialog();
|
break;
|
case PrintType.PrintReport:
|
try
|
{
|
if (MyReport.Prepare() == false)
|
return;
|
//如果打印设备为空,则让用户选择打印机。
|
if (printerSettings == null)
|
{
|
if (MyReport.ShowPrintDialog(out printerSettings) == false)
|
return;
|
}
|
MyReport.PrintPrepared(printerSettings);
|
// MyReport.PrintSettings.ShowDialog = false;
|
}
|
catch (Exception e)
|
{
|
MessageBox.Show("打印报表出错,信息如下:" + e.Message);
|
printerSettings = null;
|
return;
|
}
|
break;
|
default:
|
MessageBox.Show("没有提供此类操作!");
|
return;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 报表的打印类型
|
/// </summary>
|
public enum PrintType
|
{
|
/// <summary>
|
/// 设计报表
|
/// </summary>
|
DesignReport = 1,
|
|
/// <summary>
|
/// 预览报表
|
/// </summary>
|
PreviewReport,
|
|
/// <summary>
|
/// 打印报表
|
/// </summary>
|
PrintReport,
|
|
/// <summary>
|
/// 创建新表表
|
/// </summary>
|
CreateNewReport
|
};
|
}
|