using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace System.Windows.Forms
{
	/// <summary>
	/// GoldDataGrid 的摘要说明。
	/// </summary>
	public class GoldDataGrid:System.Windows.Forms.DataGrid
	{
		private DataGridAccess m_dataGridAccess;
		private string[] _ColumnHeadersText = new string[]{"选择","代码","部门","电话"};

		private bool _AllowShowRowHeaderSerialNo = false;	//显示行序列号
		private bool _AllowShowColHeaderSerialNo = false;	//显示列序列号


		#region 字段属性
		public bool AllowShowRowHeaderSerialNo
		{
			get
			{
				return _AllowShowRowHeaderSerialNo;
			}
			set
			{
				_AllowShowRowHeaderSerialNo = value;
			}
		}

		public bool AllowShowColHeaderSerialNo
		{
			get
			{
				return _AllowShowColHeaderSerialNo;
			}
			set
			{
				_AllowShowColHeaderSerialNo = value;
			}
		}
		#endregion

		public GoldDataGrid()
		{
			m_dataGridAccess = new DataGridAccess();
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint (e);

			#region ...
			/*
			Graphics g = e.Graphics;

			try
			{
				if(this.DataSource!=null)
				{         
					int yDelta; 

					System.Drawing .Rectangle cell=this.GetCellBounds(0,0);

					int y=cell.Top +2;

					if (this.ColumnHeadersVisible)
					{
						g.DrawString("行号", this.Font, new SolidBrush(Color.Black), 8, y -18); //
					}

					if(this.VisibleRowCount > 0)//只在有记录集时在表格中显示序号
					{                   

						CurrencyManager cm;

						cm = (CurrencyManager)this.BindingContext[this.DataSource, this.DataMember]; 

						if(cm.Count >0)
						{                       

							int nRow=-1;

							if (this.ColumnHeadersVisible)
							{
								y = 41;           //为第一行默认高度
							}
							else
							{
								y = 0;
							}

							while(nRow<0)

							{

								nRow=this.HitTest(8,y).Row ;

								y++;

							}

							int nCount=0;

							while(y<this.Height && nCount<this.VisibleRowCount )

							{

								string text = string.Format("{0}", nRow+nCount+1); 

								g.DrawString(text, this.Font, new SolidBrush(Color.Black), 10, y); 

								yDelta = this.GetCellBounds( nRow+nCount,0).Height + 1;//****表示一行高度的参数

								y += yDelta; 

								//如果下面有子行显示序号的区分显示   

								if(this.IsExpanded (nRow+nCount)&& nRow+nCount+1<cm.Count )                                 
								{

									y+=this.GetCellBounds (nRow+nCount+1,0).Height +3;

								}

								nCount++;       

							}

						}

					}

				}

			}

			catch

			{}
			*/
			#endregion

			//试过了，用水平滚动条时一下一下的击（一次一列），要重复下面的程序三次。但是，如果连续的滚动滑块，则速度受影响
			if (_AllowShowRowHeaderSerialNo)
			{
				SetRowHeaderSerialNo();
			}

			if (_AllowShowColHeaderSerialNo)
			{
				SetColHeaderSerialNo();
			}

			//标题居中
		
		}

		public int Rows
		{
			get
			{
				return m_dataGridAccess.GetRowCount(this);
			}		
		}

		public int Cols
		{
			get
			{
				return m_dataGridAccess.GetColCount(this);
			}		
		}
				
		public string[] ColumnHeadersText
		{
			get
			{
				if (_ColumnHeadersText != null)
				{
					return _ColumnHeadersText;
				}
				else
				{
					//读取绑定的默认标题
					return m_dataGridAccess.GetColumnHeadersText(this);
				}
			}
			set
			{
				_ColumnHeadersText = value;

				//绘制列标题
			}
		
		}

		public int FirstVisibleRow
		{
			get
			{
				return m_dataGridAccess.GetFirstVisibleRow(this);
			}		
		}

		public int GetRowHeaderSerialNoWidth(Font p_font)
		{
			return m_dataGridAccess.GetRowHeaderSerialNoWidth(this,p_font);
		}

		//强制为DataGrid在RowHeader上设置行序列号
		public void SetRowHeaderSerialNo()
		{
			m_dataGridAccess.SetRowHeaderSerialNo(this,this.Font);		
		}

		//强制为DataGrid在ColumnHeader上设置行序列号
		public void SetColHeaderSerialNo()
		{
			m_dataGridAccess.SetColHeaderSerialNo(this,this.HeaderFont);		
		}


	}//End Class
}//End NameSpace
