⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daprintdocument.cs

📁 是用c#实现的一个有关于报表设计的程序代码
💻 CS
📖 第 1 页 / 共 3 页
字号:
					pctFill = 0;
				}
				else
				{
					width = pctFill*width / 100;
				}

			}
			catch(Exception)
			{}

			return width;
		}


		private int adjustVerticalFill(string theFill)
		{
			int height = Math.Max(0,DefaultPageSettings.Bounds.Bottom - DefaultPageSettings.Bounds.Top - DefaultPageSettings.Margins.Top - DefaultPageSettings.Margins.Bottom);
			try
			{
				int pctFill = Convert.ToInt32(theFill);
				if (pctFill>100)
				{
					pctFill = 100;
				}
				else if (pctFill<0)
				{
					pctFill = 0;
				}
				else
				{
					height = pctFill*height / 100;
				}

			}
			catch(Exception)
			{}

			return height;
		}


		private int calculateNumberOfPages(Graphics g)
		{
			int result = 1; 

			for (int i=0;i<dynamicObjects.Length;i++)
			{
				if ( dynamicObjects[i] != null && dynamicObjects[i] is StyledTable)
				{
					StyledTable tempTable = (StyledTable)dynamicObjects[i];
					if ( tempTable.DataSource != null && mDataTables.Contains(tempTable.DataSource) )
					{

						string theTableName = tempTable.DataSource;
						DataTable podaci = (DataTable)mDataTables[theTableName];

						bool hasMore = false;
						int counted = 0;
						int numPages = 0;

						do
						{
							int relativeHeaderHeight = tempTable.CalculateRelativeHeaderHeight(g);
							int rowsForPrint = 0;
							int relativeHeight = 0;
							int relativeDataRowHeight = 0;
							int GroupByFieldIndex = -1;
							string PreviousGroupByFieldValue = "";

							if (tempTable.GroupByField != "")
							{
								if (podaci.Rows.Count > 0)
								{
									GroupByFieldIndex = podaci.Columns.IndexOf(tempTable.GroupByField);
									PreviousGroupByFieldValue = podaci.Rows[0][GroupByFieldIndex].ToString();
								}
							}

							// find out how many succeeding rows will fit into table area
							do
							{
								if (podaci.Rows.Count <= counted + rowsForPrint )
									break;

								DataRow nextRow = podaci.Rows[counted + rowsForPrint];

								if (GroupByFieldIndex != -1)
								{
									if (PreviousGroupByFieldValue != nextRow[GroupByFieldIndex].ToString())
									{
										relativeHeight += tempTable.CalculateRelativeHeaderHeight(g);
										PreviousGroupByFieldValue = nextRow[GroupByFieldIndex].ToString();
									}
								}

								relativeDataRowHeight = tempTable.CalculateRelativeDataRowHeight(nextRow,g); 
								if (relativeHeaderHeight+relativeHeight+relativeDataRowHeight<=tempTable.GetPossibleRowNumber())
								{
									relativeHeight += relativeDataRowHeight;
									rowsForPrint++;
								}
							}
							while (relativeHeaderHeight+relativeHeight+relativeDataRowHeight<=tempTable.GetPossibleRowNumber());

							counted += rowsForPrint;
							numPages++;

							hasMore = podaci.Rows.Count > counted;
						}
						while (hasMore);

						result = Math.Max(result,numPages);
					}
				}
			}
			return result;
		}


		private DataTable createSubtable(DataTable masterTable,StyledTable styledTable,int start,int length)
		{
			DataTable currentData = new DataTable();

			if ( styledTable.Columns.Length > 0 )
			{				
				for (int i=0;i<styledTable.Columns.Length;i++)
				{
					if ( masterTable.Columns.Contains(styledTable.Columns[i].Name) )
					{
						int ord = masterTable.Columns.IndexOf(styledTable.Columns[i].Name);
						currentData.Columns.Add(new DataColumn(masterTable.Columns[ord].ColumnName,masterTable.Columns[ord].DataType));
					}
					else
						throw new Exception("No such column "+ styledTable.Columns[i].Name.ToString());
				}


				for (int i=start;i<start+length;i++)
				{
					object[] newRow = new object[currentData.Columns.Count];
					for (int j=0;j<currentData.Columns.Count;j++)
					{
						int ord = masterTable.Columns.IndexOf(currentData.Columns[j].ColumnName);
						newRow[j] = masterTable.Rows[i].ItemArray[ord];
					}
					currentData.Rows.Add(newRow);
				}
				
			}
			else
			{

				for (int i=0;i<masterTable.Columns.Count;i++)
				{
					currentData.Columns.Add(new DataColumn(masterTable.Columns[i].ColumnName,masterTable.Columns[i].DataType));
				}

				for (int i=start;i<start+length;i++)
				{
					currentData.Rows.Add(masterTable.Rows[i].ItemArray);
				}
			}
			return currentData;
		}


		private DataTable SetGroupByOnDataTable(DataTable Table, string GroupByValue)
		{
			DataTable tempSourceTable = Table.Copy();
			DataTable tempTargetTable = Table.Copy();
			DataRow[] FilteredRows;
			Hashtable GroupBysDone = new Hashtable();

			tempTargetTable.Rows.Clear();

			foreach (DataRow CurrentTableRow in tempSourceTable.Rows)
			{
				if (! GroupBysDone.Contains(CurrentTableRow[GroupByValue].ToString()))
				{
					FilteredRows = tempSourceTable.Select(GroupByValue+"='"+CurrentTableRow[GroupByValue].ToString()+"'");
					FilteredRows= (DataRow[])FilteredRows.Clone();

					foreach (DataRow CurrentFilteredRow in FilteredRows)
					{
						tempTargetTable.ImportRow(CurrentFilteredRow);
					}

					GroupBysDone.Add(CurrentTableRow[GroupByValue].ToString(), "");
				}
			}

			return tempTargetTable;
		}


		private bool updateDynamicContent(Graphics g)
		{
			bool printMore = false;

			for (int i=0;i<dynamicObjects.Length;i++)
			{
				if ( dynamicObjects[i] != null )
				{
					if ( dynamicObjects[i] is TextField )
					{
						string theText = "";
						for (int j=0;j<xmlDynamicElements[i].ChildNodes.Count;j++)
						{
							if (xmlDynamicElements[i].ChildNodes[j].Name.Equals("text") )
								theText = xmlDynamicElements[i].ChildNodes[j].InnerText;
						}

						((TextField)dynamicObjects[i]).Text = ResolveParameterValues(theText);
					}
					else if ( dynamicObjects[i] is StyledTable)
					{
						StyledTable tempTable = (StyledTable)dynamicObjects[i];
						if ( tempTable.DataSource != null && mDataTables.Contains(tempTable.DataSource) )
						{
							string theTableName = tempTable.DataSource;
							DataTable podaci = (DataTable)mDataTables[theTableName];

							if (tempTable.GroupByField != "")
								podaci = this.SetGroupByOnDataTable(podaci, tempTable.GroupByField);

							try
							{
								int relativeHeaderHeight = tempTable.CalculateRelativeHeaderHeight(g);
								int rowsForPrint = 0;
								int relativeHeight = 0;
								int relativeDataRowHeight = 0;
								int GroupByFieldIndex = -1;
								string PreviousGroupByFieldValue = "";

								if (tempTable.GroupByField != "")
								{
									if (podaci.Rows.Count > 0)
									{
										GroupByFieldIndex = podaci.Columns.IndexOf(tempTable.GroupByField);
										PreviousGroupByFieldValue = podaci.Rows[0][GroupByFieldIndex].ToString();
									}
								}

								// find out how many succeeding rows will fit into table area
								//taking into account grouping because that will inject another header row
								do
								{
									if (podaci.Rows.Count <= (int)rowsPrintedSoFar[theTableName] + rowsForPrint )
										break;

									DataRow nextRow = podaci.Rows[(int)rowsPrintedSoFar[theTableName] + rowsForPrint];

									if (GroupByFieldIndex != -1)
									{
										if (PreviousGroupByFieldValue != nextRow[GroupByFieldIndex].ToString())
										{
											relativeHeight += tempTable.CalculateRelativeHeaderHeight(g);
											PreviousGroupByFieldValue = nextRow[GroupByFieldIndex].ToString();
										}
									}

									relativeDataRowHeight = tempTable.CalculateRelativeDataRowHeight(nextRow,g); 
									if (relativeHeaderHeight+relativeHeight+relativeDataRowHeight<=tempTable.GetPossibleRowNumber())
									{
										relativeHeight += relativeDataRowHeight;
										rowsForPrint++;
									}
								}
								while (relativeHeaderHeight+relativeHeight+relativeDataRowHeight<=tempTable.GetPossibleRowNumber());
					
								// create subtable for printing
								tempTable.Data = createSubtable(podaci,tempTable,(int)rowsPrintedSoFar[theTableName],rowsForPrint);
								rowsPrintedSoFar[theTableName] = rowsForPrint + (int)rowsPrintedSoFar[theTableName];

								// if there are more rows, go on with printing
								if ( podaci.Rows.Count > (int)rowsPrintedSoFar[theTableName])
									printMore = true;
							}
							catch (Exception e)
							{
								// print exception text in table header
								printMore = false;
								StyledTableColumn[] kolone = new StyledTableColumn[1];
								kolone[0] = new StyledTableColumn();
								kolone[0].Label = e.Message;
								tempTable.Columns = kolone;
							}
						}
					}
				}
			}
			return printMore;
		}


		#endregion

		#region ICustomPaint Declarations

		/// <summary>
		/// Gets a collection of <see cref="daReport.ICustomPaint">daReport.ICustomPaint</see> objects
		/// which display dynamic data. This is only the <see cref="daReport.StyledTable">daReport.StyledTable</see>
		/// object at the moment.
		/// </summary>
		[Browsable(false)]
		public ICustomPaint[] DynamicObjects
		{
			get {return this.dynamicObjects;}
			set {this.dynamicObjects=value; }
		}
		

		/// <summary>
		/// Gets a collection of <see cref="daReport.ICustomPaint">daReport.ICustomPaint</see> objects
		/// which only display static data such as TextField, PictureBox, etc
		/// </summary>
		[Browsable(false)]
		public ICustomPaint[] StaticObjects
		{
			get {return this.staticObjects;}
			set {this.staticObjects=value; }
		}

		
		#endregion

		#region Creators and Destructor

		/// <summary>
		/// Initializes a new instance of the DaPrintDocument class.
		/// </summary>
		public DaPrintDocument()
		{
			try
			{
				if (PrinterSettings.InstalledPrinters.Count == 0)
				{
					MessageBox.Show("You do not appear to have any printers installed", "daReport");
				}
				else
				{
					// This call is required by the Windows.Forms Form Designer.
					InitializeComponent();
			
					int[] size  = Paper.GetPaperSize(paperType);
					DefaultPageSettings.PaperSize = new PaperSize("",size[0],size[1]);
					DefaultPageSettings.Margins = new Margins(50,50,50,50);
					DefaultPageSettings.Landscape = false;
					declaredParameters = new Parameters();
					initSystemParameters();
					parameterValues = new Hashtable();
					mDataTables = new Hashtable();
					theColumns = new Hashtable();
					theCharts = new Hashtable();
					rowsPrintedSoFar = new Hashtable();
				}
			}
			catch (Exception ex)
			{
				if (ex.Message == "The RPC server is unavailable")
				{
					MessageBox.Show("It appears the print spooler service is not running", "daReport");
				}
				else
				{
					MessageBox.Show(ex.Message);
				}
			}
		}	


		/// <summary>
		/// Initializes a new instance of the DaPrintDocument class.
		/// </summary>
		public DaPrintDocument(bool theMode):this() 
		{			
			designMode = theMode;
		}


		/// <summary>
		/// Initializes a new instance of the DaPrintDocument class.
		/// </summary>
		public DaPrintDocument(Hashtable parameters):this()
		{
			parameterValues = parameters;
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (staticObjects != null)
				{
					for (int i =0;i<staticObjects.Length;i++)
					{
						if ( staticObjects[i] is PictureBox )
							((PictureBox)staticObjects[i]).Dispose();
					}
				}

				if (designLicense != null)
				{
					designLicense.Dispose();
					designLicense = null;
				}

				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		
		/// <summary>
		/// Gives DaPrintDocument the opportunity to finalize any child resources
		/// </summary>
		~DaPrintDocument()
		{
			Dispose();
		}

		
		#region Component Designer generated code
		
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// DaPrintDocument
			// 
			this.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.DaPrintDocument_BeginPrint);
			this.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.DaPrintDocument_PrintPage);
		}
		
		
		#endregion

		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -