為了遵循類的單一職責原則,破除它們之前的相互依賴性,在下面我將提供一個實現水晶報表顯示的範例,這個範例的靈感來源於 MessageBox類,只提供了幾個靜態公共方法用於顯示報表:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using ChiefValueAccounting.Util;
namespace ChiefValueAccounting.Win.Print
{
/// <summary>
/// 顯示與列印水晶報表。
/// </summary>
public class CrystalReportBox
{
private static CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer;
private static System.Windows.Forms.Form frmReport;
static CrystalReportBox()
{
InitializeComponent();
}
private static void InitializeComponent()
{
frmReport = new Form();
crystalReportViewer = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
frmReport.SuspendLayout();
//
// crystalReportViewer
//
crystalReportViewer.ActiveViewIndex = -1;
crystalReportViewer.DisplayGroupTree = false;
crystalReportViewer.Dock = System.Windows.Forms.DockStyle.Fill;
crystalReportViewer.Location = new System.Drawing.Point(0, 0);
crystalReportViewer.Name = "crystalReportViewer";
crystalReportViewer.ReportSource = null;
crystalReportViewer.Size = new System.Drawing.Size(708, 482);
crystalReportViewer.TabIndex = 0;
//
// frmReport
//
frmReport.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
frmReport.ClientSize = new System.Drawing.Size(708, 482);
frmReport.Controls.Add(crystalReportViewer);
frmReport.Name = "FormPrintReportEx";
frmReport.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
frmReport.Text = "Report Viewer";
frmReport.WindowState = System.Windows.Forms.FormWindowState.Maximized;
frmReport.MaximizeBox = false;
frmReport.MinimizeBox = false;
frmReport.ShowInTaskbar = false;
frmReport.FormBorderStyle = FormBorderStyle.FixedDialog;
frmReport.ResumeLayout(false);
}
/// <summary>
/// 顯示水晶報表,要求調用方設定好報表對象的所有屬性。
/// </summary>
/// <param name="reportClass">強類型的報表對象</param>
public static void ShowReport(CrystalDecisions.CrystalReports.Engine.ReportClass reportClass, System.Windows.Forms.IWin32Window owner)
{
try
{
crystalReportViewer.ReportSource = reportClass;
frmReport.ShowDialog(owner);
}
catch(Exception ex)
{
SimpleLogger.Log(ex);
Global.ShowError(ex);
}
}
/// <summary>
/// 顯示水晶報表,要求調用方設定好報表對象的所有屬性。
/// </summary>
/// <param name="reportDocument">水晶報表文檔對象</param>
public static void ShowReport(CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument, System.Windows.Forms.IWin32Window owner)
{
try
{
crystalReportViewer.ReportSource = reportDocument;
frmReport.ShowDialog(owner);
}
catch(Exception ex)
{
SimpleLogger.Log(ex);
Global.ShowError(ex);
}
}
/// <summary>
/// 顯示水晶報表。
/// </summary>
/// <param name="reportName">報表名稱</param>
/// <param name="dataSource">報表數據源</param>
public static void ShowReport(string reportName, object dataSource, System.Windows.Forms.IWin32Window owner)
{
try
{
ReportDocument rd = new ReportDocument();
rd.Load(reportName);
rd.SetDataSource(dataSource);
frmReport.ShowDialog(owner);
}
catch(Exception ex)
{
SimpleLogger.Log(ex);
Global.ShowError(ex);
}
}
}
}
也就是說,對於報表中的一些業務邏輯設定,是不應該放到這個顯示類中的,而是應該由調用方負責進行設定與刷新。