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

📄 styledtable.cs

📁 是用c#实现的一个有关于报表设计的程序代码
💻 CS
📖 第 1 页 / 共 4 页
字号:
					mRegion.X = mRegion.X - value + mRegion.Width;
					mRegion.Width = value;									
				}
				else if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Center)
				{
					mRegion.X = mRegion.X - value/2 + mRegion.Width/2;
					mRegion.Width = value;									
				}
				

				if (mColumns.Length>0)
				{
					int sumOfWidths = 0;
					for (int i=0;i<mColumns.Length;i++)
					{
						sumOfWidths += mColumns[i].Width;
					}

					if (sumOfWidths<mRegion.Width)
						mColumns[mColumns.Length-1].Width += mRegion.Width - sumOfWidths;
					else
					{
						if (sumOfWidths-mColumns[mColumns.Length-1].Width < mRegion.Width)
							mColumns[mColumns.Length-1].Width -= sumOfWidths - mRegion.Width;
						else
						{
							double ratio = (double)mRegion.Width / sumOfWidths ;
							for (int i=0;i<mColumns.Length;i++)
							{
								mColumns[i].Width = (int)(ratio*mColumns[i].Width);
							}

						}
					}
				}
			}
		}


		/// <summary>
		/// The X coordinate of the left-upper corner of the StyledTable
		/// </summary>
		[Category("Layout"), Description("The X coordinate of the left-upper corner of the element.")]
		public override int X
		{
			get {return mRegion.X;}
			set 
			{
				if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.None)
					mRegion.X = value;
				else if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Right)
				{
					mRegion.Width = mRegion.Width + mRegion.X - value;
					mRegion.X = value;					
				}
				else if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Center)
				{
					mRegion.Width = mRegion.Width + 2*(mRegion.X - value);
					mRegion.X = value;					
				}
			}
		}


		/// <summary>
		/// The Y coordinate of the left-upper corner of the StyledTable
		/// </summary>
		[Category("Layout"), Description("The Y coordinate of the left-upper corner of the element.")]
		public override int Y
		{
			get {return mRegion.Y;}
			set 
			{
				if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.None)
					mRegion.Y = value;
				else if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.Bottom)
				{
					mRegion.Height = mRegion.Height + mRegion.Y - value;
					mRegion.Y = value;					
				}
				else if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.Middle)
				{
					mRegion.Height = mRegion.Height + 2*(mRegion.Y - value);
					mRegion.Y = value;					
				}
			}
		}


		#endregion

		#region Private Properties

		private StyledTableColumn[] createColumns(DataTable masterTable)
		{
			StyledTableColumn[] cols = new StyledTableColumn[masterTable.Columns.Count];
			for (int i=0;i<masterTable.Columns.Count;i++)
			{
				cols[i] = new StyledTableColumn();
				cols[i].Name = masterTable.Columns[i].ColumnName;
				cols[i].Label = masterTable.Columns[i].ColumnName;
			}
			return cols;
		}


		private int drawHeader(Graphics g, Rectangle TargetRegion, int DrawnSoFar, string GroupByTitle)
		{
			int MaxLinesDrawn = 0;
			int leftBorder = mRegion.X;

			ArrayList[] headerLabels = new ArrayList[mColumns.Length];
			for (int i=0;i<mColumns.Length;i++)
			{
				if (mColumns[i].Visible)
				{
					string tmpValue = mColumns[i].Label;
					string theLine = "";
					headerLabels[i] = new ArrayList();
					bool WillTextWrap = false;
					do
					{
						WillTextWrap = splitString(ref tmpValue,ref theLine,mColumns[i].Width,g,mHeaderFont);
						headerLabels[i].Add(theLine);
					}
					while(WillTextWrap);

					if (headerLabels[i].Count > MaxLinesDrawn)
						MaxLinesDrawn = headerLabels[i].Count;
						
					if (! this.mColumnTotals.ContainsKey(mColumns[i].Name))
					{
						if (mColumns[i].CalculateTotal)
							this.mColumnTotals.Add(mColumns[i].Name, 0D);
					}
				}
			}

			// draw header background
			if (this.mDisplayGroupTitle)
			{
				MaxLinesDrawn += 1;
				g.FillRectangle(new SolidBrush(mHeaderBackgroundColor),TargetRegion.X,TargetRegion.Y,TargetRegion.Width,(mCellHeight*MaxLinesDrawn));

				if (GroupByTitle == "")
					g.DrawString("Group",mHeaderFont,new SolidBrush(this.mHeaderFontColor),TargetRegion.X,TargetRegion.Y);
				else
					g.DrawString(GroupByTitle,mHeaderFont,new SolidBrush(this.mHeaderFontColor),TargetRegion.X,TargetRegion.Y);
			}
			else
			{
				g.FillRectangle(new SolidBrush(mHeaderBackgroundColor),TargetRegion.X,TargetRegion.Y,TargetRegion.Width,mCellHeight*MaxLinesDrawn);	
			}
			//g.DrawString(theLines[j].ToString(),mHeaderFont,new SolidBrush(this.mHeaderFontColor),yPos,TargetRegion.Top+mCellHeight*j+mCellHeight/2-sf.Height/2);

			for (int i=0;i<mColumns.Length;i++)
			{
				if (mColumns[i].Visible)
				{
					ArrayList theLines = headerLabels[i];

					for (int j=0;j<theLines.Count;j++)
					{
						SizeF sf = g.MeasureString(theLines[j].ToString(),mHeaderFont);
						float xPos = leftBorder+1;

						switch (mColumns[i].Alignment)
						{
							case StyledTableColumn.AlignmentType.Left:
								xPos = leftBorder + 1;
								break;

							case StyledTableColumn.AlignmentType.Center:
								xPos = leftBorder + ((mColumns[i].Width / 2) - (sf.Width / 2));
								break;

							case StyledTableColumn.AlignmentType.Right:
								xPos = leftBorder + mColumns[i].Width - sf.Width;
								break;

							default:
								xPos = leftBorder + 1;
								break;
						}
						if (this.mDisplayGroupTitle)
							g.DrawString(theLines[j].ToString(),mHeaderFont,new SolidBrush(this.mHeaderFontColor), xPos, TargetRegion.Top+mCellHeight*(j+1)+mCellHeight/2-sf.Height/2);
						else
							g.DrawString(theLines[j].ToString(),mHeaderFont,new SolidBrush(this.mHeaderFontColor),xPos,TargetRegion.Top+mCellHeight*j+mCellHeight/2-sf.Height/2);
					}
					leftBorder += mColumns[i].Width;
				}
			}

			if (DrawnSoFar == 0 && this.mTableBorders.DisplayTopBorder)
			{
				g.DrawLine(new Pen(this.mBorderColor,1), TargetRegion.X, TargetRegion.Y, TargetRegion.X+TargetRegion.Width, TargetRegion.Y);
			}

			if (this.mLineUnderNewGroup)
				g.DrawLine(new Pen(Color.Black,1), TargetRegion.X, TargetRegion.Y+mCellHeight, TargetRegion.X+TargetRegion.Width, TargetRegion.Y+mCellHeight);

			return DrawnSoFar+MaxLinesDrawn;
		}
		
		
		private int drawDataRow(Graphics g, int theRow, int drawnSoFar)
		{
			if (theRow > mData.Rows.Count || theRow < 0 || drawnSoFar >= GetPossibleRowNumber()) return drawnSoFar;

			int maxDrawnRows = 0;			
			int	topBorder = mRegion.Top + drawnSoFar*mCellHeight;

			if (this.mDisplayGridLines)
				g.DrawLine(new Pen(this.mBorderColor,1),mRegion.X, topBorder, mRegion.Right, topBorder);

			int leftBorder = mRegion.X;
			for (int i=0;i<mData.Rows[theRow].ItemArray.Length;i++)
			{
				if (mColumns[i].Visible)
				{
					object theObject = mData.Rows[theRow].ItemArray.GetValue(i);				

					if (this.mColumnTotals.ContainsKey(mColumns[i].Name))
						this.mColumnTotals[mColumns[i].Name] = Convert.ToDecimal(this.mColumnTotals[mColumns[i].Name].ToString()) + Convert.ToDecimal(theObject.ToString());

					// format object as string
					string tmpValue = FormatValue(theObject,mData.Columns[i].DataType, mColumns[i].FormatMask);

					string theLine = "";
					int drawnRowsCounter = 0;

					bool hasMore = false;
					do
					{
						hasMore = splitString(ref tmpValue,ref theLine,mColumns[i].Width,g,mDataFont);
						SizeF sf = g.MeasureString(theLine,mDataFont);

						float yPos = leftBorder+1;

						switch (mColumns[i].Alignment)
						{
							case StyledTableColumn.AlignmentType.Left:
								yPos = leftBorder + 1;
								break;

							case StyledTableColumn.AlignmentType.Center:
								yPos = leftBorder + ((mColumns[i].Width / 2) - (sf.Width / 2));
								break;

							case StyledTableColumn.AlignmentType.Right:
								yPos = leftBorder + mColumns[i].Width - sf.Width;
								break;

							default:
								yPos = leftBorder + 1;
								break;
						}

						g.DrawString(theLine,mDataFont,new SolidBrush(this.mDataFontColor),yPos,topBorder+mCellHeight*drawnRowsCounter+mCellHeight/2-sf.Height/2);	
						drawnRowsCounter++;
					}
					while ( hasMore );

					leftBorder += mColumns[i].Width;

					if (drawnRowsCounter>maxDrawnRows)
						maxDrawnRows = drawnRowsCounter;
				}
			}

			return drawnSoFar+maxDrawnRows;
		}


		private int DrawTotals(Graphics g, int drawnSoFar)
		{
			if (drawnSoFar >= GetPossibleRowNumber()) return drawnSoFar;

			int maxDrawnRows = 0;			
			int	topBorder = mRegion.Top + drawnSoFar*mCellHeight;

			if (this.mDisplayGridLines)
			{
				g.DrawLine(new Pen(this.mBorderColor,1),mRegion.X, topBorder, mRegion.Right, topBorder);
				g.DrawLine(new Pen(this.mBorderColor,1),mRegion.X, topBorder+2, mRegion.Right, topBorder+2);
			}

			int leftBorder = mRegion.X;
			for (int i=0;i<mColumns.Length;i++)
			{
				if (mColumns[i].Visible)
				{
					object theObject = new Object();

					// if column is present in the Totals HashTable, then we grab value, else just use empty string
					if (this.mColumnTotals.ContainsKey(mColumns[i].Name))
						theObject = Convert.ToDecimal(this.mColumnTotals[mColumns[i].Name].ToString());
					else
						theObject = "";

					// format object as string
					string tmpValue = FormatValue(theObject,mData.Columns[i].DataType, mColumns[i].FormatMask);

					string theLine = "";
					int drawnRowsCounter = 0;

					bool hasMore = false;
					do
					{
						hasMore = splitString(ref tmpValue,ref theLine,mColumns[i].Width,g,mDataFont);
						SizeF sf = g.MeasureString(theLine,mDataFont);

						float yPos = leftBorder+1;

						switch (mColumns[i].Alignment)
						{
							case StyledTableColumn.AlignmentType.Left:
								yPos = leftBorder + 1;
								break;

							case StyledTableColumn.AlignmentType.Center:
								yPos = leftBorder + ((mColumns[i].Width / 2) - (sf.Width / 2));
								break;

							case StyledTableColumn.AlignmentType.Right:
								yPos = leftBorder + mColumns[i].Width - sf.Width;
								break;

							default:
								yPos = leftBorder + 1;
								break;
						}

						g.DrawString(theLine,mDataFont,new SolidBrush(this.mDataFontColor),yPos,topBorder+mCellHeight*drawnRowsCounter+mCellHeight/2-sf.Height/2);	
						drawnRowsCounter++;
					}
					while ( hasMore );

					leftBorder += mColumns[i].Width;

					if (drawnRowsCounter>maxDrawnRows)
						maxDrawnRows = drawnRowsCounter;
				}
			}

			g.DrawLine(new Pen(this.mBorderColor,1),mRegion.X, topBorder+this.mCellHeight-2, mRegion.Right, topBorder+this.mCellHeight-2);

			return drawnSoFar+maxDrawnRows;
		}


		//   formating datatable objects
		//   TODO : improve this function 
		//   for instance : dates with respect to user input locale, etc.
		private string FormatValue(object theObject,Type tip, string FormatMask)
		{
			string theValue = "";
			if ( theObject == DBNull.Value )
			{
				theValue = "";
			}
			else
			{
				try
				{
					if (FormatMask != "")
					{
						theValue = string.Format("{0:"+FormatMask+"}", theObject);
					}
					else
					{
						if (tip == System.Type.GetType("System.DateTime") )
						{
							theValue = ((DateTime)theObject).ToString("dd.MM.yyyy.");
						}
						else if (tip == System.Type.GetType("System.Decimal") )
						{
							theValue = ((Decimal)theObject).ToString("f02");
						}
						else if (tip == System.Type.GetType("System.UInt32") || tip == System.Type.GetType("System.UInt16") || tip == System.Type.GetType("System.UInt64") || tip == System.Type.GetType("System.Int16") || tip == System.Type.GetType("System.Int32") || tip == System.Type.GetType("System.Int64"))
						{
							theValue = theObject.ToString();
						}
						else
						{
							theValue = theObject.ToString();
						}
					}
				}
				catch
				{
					if (tip == System.Type.GetType("System.DateTime") )
					{
						theValue = ((DateTime)theObject).ToString("dd.MM.yyyy.");
					}
					else if (tip == System.Type.GetType("System.Decimal") )
					{
						theValue = ((Decimal)theObject).ToString("f02");
					}
					else if (tip == System.Type.GetType("System.UInt32") || tip == System.Type.GetType("System.UInt16") || tip == System.Type.GetType("System.UInt64") || tip == System.Type.GetType("System.Int16") || tip == System.Type.GetType("System.Int32") || tip == System.Type.GetType("System.Int64"))
					{
						theValue = theObject.ToString();
					}
					else
					{
						theValue = theObject.ToString();
					}
				}
				
			}
			return theValue;
		}

		
		private bool splitString(ref string theSource,ref string theResult,int theWidth,Graphics g,Font theFont)
		{
			int lastBlank = -1;
			string tmpBuffer = "";
			bool hasMore = false;

			for (int i=0;i<theSource.Length;i++)
			{

⌨️ 快捷键说明

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