📄 sharpreportengine.cs
字号:
//
// SharpDevelop ReportEditor
//
// Copyright (C) 2005 Peter Forstmeier
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Peter Forstmeier (Peter.Forstmeier@t-online.de)
using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using SharpReportCore;
/// <summary>
/// This Class contains the basic Functions to handle reports
/// </summary>
/// <remarks>
/// created by - Forstmeier Peter
/// created on - 31.08.2005 16:21:38
/// </remarks>
namespace SharpReportCore {
public class SharpReportEngine : object {
private PaintArea paintArea = null;
private ConnectionObject connectionObject = null;
public event SharpReportEventHandler NoData;
public event SharpReportParametersEventHandler ParametersRequest;
public SharpReportEngine() {
if (SharpReportCore.GlobalValues.IsValidPrinter() == false) {
InvalidPrinterException ex = new InvalidPrinterException(new PrinterSettings());
throw ex;
}
}
#region ParameterHandling
private bool CheckReportParameters (ReportModel model,ReportParameters reportParameters) {
if (model.ReportSettings.ReportType != GlobalEnums.enmReportType.FormSheet) {
if (this.connectionObject == null) {
if (model.ReportSettings.ConnectionString != "") {
this.connectionObject = new ConnectionObject (model.ReportSettings.ConnectionString,"","");
}
if (reportParameters != null) {
this.connectionObject = reportParameters.ConnectionObject;
}
if (this.connectionObject.Connection != null) {
return true;
} else {
throw new SharpReportException("SharpReportEngine:CheckReportParameters : No valid Connection");
}
}
} else {
return true;
}
return false;
}
void GrapSqlParameters (ReportSettings settings) {
if (settings.SqlParametersCollection != null && settings.SqlParametersCollection.Count > 0) {
if (this.ParametersRequest != null) {
SharpReportParametersEventArgs e = new SharpReportParametersEventArgs();
e.SqlParametersCollection = settings.SqlParametersCollection;
e.ReportName = settings.ReportName;
ParametersRequest (this,e);
}
}
}
void SetSqlParameters (ReportModel model,AbstractParametersCollection sqlParams) {
if ((sqlParams == null)||(sqlParams.Count == 0)) {
return;
}
model.ReportSettings.SqlParametersCollection.Clear();
model.ReportSettings.SqlParametersCollection.AddRange(sqlParams);
}
void SetCustomSorting (ReportModel model,ColumnCollection sortParams) {
if ((sortParams == null)||(sortParams.Count == 0)) {
return;
}
model.ReportSettings.SortColumnCollection.Clear();
model.ReportSettings.SortColumnCollection.AddRange(sortParams);
}
private void ApplyReportParameters (ReportModel model,ReportParameters parameters){
if ((model == null)||(parameters == null )){
throw new ArgumentNullException("SharpReportEngine:ApplyReportParameters");
}
SetSqlParameters (model,parameters.SqlParameters);
SetCustomSorting (model,parameters.SortColumnCollection);
}
#endregion
#region Setup for print/preview
private bool CheckForPushModel (ReportModel model) {
if (model.ReportSettings.DataModel == GlobalEnums.enmPushPullModel.PushData) {
return true;
} else {
return false;
}
}
private DataManager SetupDataContainer (ReportSettings settings) {
if (settings.ReportType == GlobalEnums.enmReportType.DataReport) {
if (settings.CommandText != null) {
try {
GrapSqlParameters (settings);
if (this.connectionObject != null) {
DataManager container = new DataManager(this.connectionObject,
settings);
if (container.DataBind() == true) {
return container;
} else {
return null;
}
}else {
throw new NullReferenceException("SetupContainer:connectionObject is missing");
}
}
catch (System.Data.OleDb.OleDbException e) {
MessageBox.Show (e.Message,"SharpReportEngine:SetupDataContainer");
}
catch (Exception) {
throw;
}
}
}
return null;
}
protected ColumnCollection CollectFieldsFromModel(ReportModel model){
ColumnCollection col = new ColumnCollection();
if (model == null) {
return col;
}
foreach (BaseSection section in model.SectionCollection){
for (int i = 0;i < section.Items.Count ;i ++ ) {
IItemRenderer item = section.Items[i];
BaseDataItem baseItem = item as BaseDataItem;
if (baseItem != null) {
col.Add(new AvailableField(baseItem.ColumnName));
}
}
}
return col;
}
protected SharpReportCore.AbstractRenderer SetupStandartRenderer (ReportModel model) {
AbstractRenderer abstr = null;
switch (model.ReportSettings.ReportType) {
//FormSheets reports
case GlobalEnums.enmReportType.FormSheet:
abstr = new RendererFactory().Create (model,null);
break;
//Databased reports
case GlobalEnums.enmReportType.DataReport :
DataManager dataManager = SetupDataContainer (model.ReportSettings);
if (dataManager != null) {
if (dataManager.DataSource != null) {
abstr = new RendererFactory().Create (model,dataManager);
}
} else {
if (NoData != null) {
SharpReportEventArgs e = new SharpReportEventArgs();
e.PageNumber = 0;
e.Cancel = false;
NoData (this,e);
if (e.Cancel == true) {
// If we cancel, we have to create an instance of any kind of renderer
//to set the cancel flag to true
abstr = new RendererFactory().Create (model,null);
abstr.Cancel = true;
} else {
// Print the report only as Formsheet -> only Text and Graphic Items
abstr = new RendererFactory().Create (model,null);
}
}
}
break;
default:
throw new SharpReportException ("SharpReportmanager:SetupRenderer -> Unknown Reporttype");
}
return abstr;
}
protected SharpReportCore.AbstractRenderer SetupPushDataRenderer (ReportModel model,
DataTable dataTable) {
if (model.ReportSettings.ReportType != GlobalEnums.enmReportType.DataReport) {
throw new ArgumentException("PrepareForPushDataReport No valid ReportModel");
}
if (model.ReportSettings.DataModel != GlobalEnums.enmPushPullModel.PushData) {
throw new ArgumentException("PrepareForPushDataReport No valid ReportType");
}
AbstractRenderer abstr = null;
DataManager dataManager = new DataManager (dataTable,model.ReportSettings);
dataManager.DataBind();
if (dataManager != null) {
abstr = new RendererFactory().Create (model,dataManager);
return abstr;
}
return null;
}
#endregion
/// <summary>
/// Creates an <see cref="AbstractRenderer"></see>
/// any Class deriving from this can be
/// used to get a <see cref="System.Drawing.Printing.PrintDocument"></see>
/// </summary>
/// <param name="model"><see cref="ReportModel"></see></param>
/// <returns></returns>
protected AbstractRenderer AbstractRenderer (ReportModel model) {
if (model == null) {
throw new ArgumentNullException("PrintableDocument:ReportModel");
}
AbstractRenderer abstr = SetupStandartRenderer(model);
return abstr;
}
#region Parameter Handling
///<summary>Loads the report, and initialise <see cref="ReportParameters"></see>
/// Fill the <see cref="AbstractParametersCollection"></see> with <see cref="SqlParameters">
/// this is an easy way to ask the report for desired paramaters</see></summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -