using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using FlexGrid = AxMSHierarchicalFlexGridLib.AxMSHFlexGrid;
using ljs;
using ljs.MyClass.Menu ;
namespace ljs
{
	/// <summary>
	/// MyFlexGrid 的摘要说明。
	/// </summary>
	/// 
	/// <summary>
	/// 单元格输入类型
	/// </summary>
	public enum eInputType {TextBox=0,CheckBox=1,ComboBox=2,Command=3,DateList=4,None=9}

	public delegate void FlexGridCell_Click(object sender, System.EventArgs e);
	public delegate void FlexGridCell_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e);
	public delegate void FlexGridCell_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e);
	public delegate void FlexGridCell_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e);
	public delegate void FlexGridCell_Enter(object sender, System.EventArgs e);
	public delegate void FlexGridCell_Leave(object sender, System.EventArgs e);

	public delegate void FlexGridCell_MouseEnter(object sender, System.EventArgs e);
	public delegate void FlexGridCell_MouseLeave(object sender, System.EventArgs e);
	public delegate void FlexGridCell_TextChanged(object sender, System.EventArgs e);
	public delegate void FlexGridCell_DoubleClick(object sender, System.EventArgs e);
	public delegate void FlexGridCell_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e);
	public delegate void FlexGridCell_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e);
	public delegate void FlexGrid_Scroll(object sender, System.EventArgs e);
	public delegate void FlexGridRow_Click(object sender, System.EventArgs e);
	public delegate void FlexGridGrid_Click(object sender, System.EventArgs e);
	public delegate void FlexGridRow_DoubleClick(object sender, System.EventArgs e);
	//	public delegate void FlexGridRow_KeyPress( object sender , AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_KeyPressEvent e );
	//	public delegate void FlexGridRow_MouseDown(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseDownEvent e);
	
	//	public delegate void Split_Drag(object sender, System.Windows.Forms.MouseEventArgs e) ;
	//	public event AlarmEventHandler Alarm;
	public class MyFlexGrid : System.Windows.Forms.UserControl
	{

	
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;private FlexGrid grd_Grid;
		private System.Windows.Forms.TextBox txt_Cell;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem11;
		private System.Windows.Forms.MenuItem Mnu_AddRow;
		private System.Windows.Forms.MenuItem Mnu_DelRow;
		private System.Windows.Forms.MenuItem Mnu_RowsAdd;
		private System.Windows.Forms.MenuItem Mnu_GridClear;
		private System.Windows.Forms.MenuItem Mnu_ColHide;
		private System.Windows.Forms.MenuItem Mnu_ColAllShow;
		private System.Windows.Forms.MenuItem Mnu_ColWidth;
		private System.Windows.Forms.MenuItem Mnu_ColWidthAuto;
		private System.Windows.Forms.MenuItem Mnu_ColWidthAVG;
		private System.Windows.Forms.MenuItem Mnu_ColSum;
		private System.Windows.Forms.MenuItem Mnu_Copy;
		private System.Windows.Forms.MenuItem Mnu_Cut;
		private System.Windows.Forms.MenuItem Mnu_Paste;
		private System.Windows.Forms.MenuItem Mnu_SaveAs;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem Mnu_ColASC;
		private System.Windows.Forms.MenuItem Mnu_ColDesc;

		#region 自定义新事件
		public event FlexGridCell_Click Cell_Click ;
		public event FlexGridCell_MouseDown Cell_MouseDown ;
		public event FlexGridCell_MouseUp Cell_MouseUp ;
		public event FlexGridCell_KeyPress Cell_KeyPress ;
		public event FlexGridCell_Enter Cell_Enter ;
		public event FlexGridCell_Leave Cell_Leave ;

		public event FlexGridCell_MouseEnter Cell_MouseEnter ;
		public event FlexGridCell_MouseLeave Cell_MouseLeave ;
		public event FlexGridCell_TextChanged Cell_TextChanged ;
		public event FlexGridCell_DoubleClick Cell_DoubleClick ;
		public event FlexGridCell_KeyUp Cell_KeyUp ;
		public event FlexGridCell_KeyDown Cell_KeyDown ;
		public event FlexGrid_Scroll Scroll ;
		public event FlexGridRow_Click Row_Click ;
		public event FlexGridGrid_Click Grid_Click ;
		public event FlexGridRow_DoubleClick Row_DoubleClick ;
		//		public event FlexGridRow_KeyPress Row_KeyPress ;
		//		public event FlexGridRow_MouseDown Row_MouseDown ;

		#endregion
		#region 属性变量
		RightMenu Menu=new RightMenu()  ;
		private eInputType inputType=0;

		private int ActiveRow; 
		private int ActiveCol;
		private string setcolWidth; //="100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100"; 
		private int row; 
		private int col;
		private int rows=2; 
		private int cols=2; 
		private int cellColLength=0; //单元格列间距
		private int cellRowLength=0; //单元格行ou间距
		private int cellWidth; 
		private int cellTop; 
		private int cellLeft; 
		private int cellHeight; 
		private bool multiHead=false;
		private bool cellMultiLine=false;
		private Color fixedBackColor ; 
		private Color cellBackColor=Color.White ; 
		private Color cellForeColor=Color.Black ; 
		//		private int gridWidth; 
		//		private int gridHeight; 
		private string cellTag;
		//		private System.Windows.Forms.Label lblBorder;
		//		private System.Windows.Forms.TextBox Cell;
		private bool gridStyle=true;
		private bool borderStyle=false;  //边框样式
		//		private System.Windows.Forms.Label Split;
		private bool enterToTab=true;
		//		private int leftCol=0 ;  //左边第一列边缘的坐标
		//		Label lbltemp;
		//		int curCol;
		private string formatString;
		private string colCommanFormat;   //列控制格式字符串
		//		private string CurColComman;      //当前列控制命令字符串  分解colCommanFormat的列
		private FlexGrid grd_Caption;
		private System.Windows.Forms.ContextMenu RigtMenu;
		private string formatString1;
		private string detailFormat;
		private bool colLocked=false;
		private bool onlyRead=false;
		private System.Windows.Forms.CheckBox chk_YesOrNo;
		private ToolTip nToolTip=new ToolTip();

		#endregion 



	#region 获得设置属性
		/// <summary>
		/// 单元格输入类型 TextBox=0,CheckBox=1,ComboBox=2
		/// </summary>
		public eInputType InputType
		{
			set { inputType=value;} 
		}
		public object GetCaption 
		{
			get {return grd_Caption ;} 
		}
		public FlexGrid GetGrid 
		{
			get {return  grd_Grid;}
		}
		/// <summary>
		/// 得到编辑单元格对象txt_Cell
		/// </summary>
		public TextBox GetEditCell 
		{
			get {return  txt_Cell;}
		}
		/// <summary>
		/// 设置/返回一个范围的单元的起始行 
		/// </summary>
		public int RowSel
		{
			get {return grd_Grid.RowSel ;} 
			set { grd_Grid.RowSel=value;} 
		}
		/// <summary>
		///  设置/返回某个范围单元的起始列。
		/// </summary>
		public int ColSel
		{
			get {return grd_Grid.ColSel ;} 
			set { grd_Grid.ColSel=value;} 
		}


		/// <summary>
		/// 网格是否是双层可合并的标题头。
		/// </summary>
		public bool MultiHead
		{
			get {return multiHead ;} 
			set { multiHead=value;} 
		}
		
		/// <summary>
		/// 设置单元格是否可以换行编辑。
		/// </summary>

		public bool CellMultiLine
		{
			get {return cellMultiLine ;} 
			set 
			{ 
				cellMultiLine=value;
				txt_Cell.Multiline =cellMultiLine;
			} 
		}
		/// <summary>
		/// 设置单元格是否可以编辑 true =只读 false=可编辑
		/// </summary>
		public bool ColLocked
		{
			get {return colLocked ;} 
			set { colLocked=value;} 
		}

		/// <summary>
		/// 设置网格格是否是只读 true =只读 false=可编辑
		/// </summary>
		public bool OnlyRead
		{
			get {return onlyRead ;} 
			set { onlyRead=value;} 
		}

		///<summary>
		/// 设置网格标题头0行格式
		/// </summary>
		public string FormatString
		{ 
			get {return formatString;} 
			set 
			{
				formatString =value;
				if (formatString!=null)
				{
					string [] sArr=null ;
					sArr=formatString.Split('|') ;
					int cs=sArr.Length;
					if (cs>cols)  {cols=cs;}
					grd_Caption.set_Cols(0,cs);
					grd_Grid.set_Cols(0,cs);

					//					if (multiHead==true)
					//					{
					grd_Caption.set_MergeRow(0, true) ; grd_Caption.set_MergeRow(1, true);
					for (int c=0;c<cs;c++)
					{
						grd_Caption.set_MergeCol(c, true);
						grd_Caption.Col =c;
						grd_Caption.Row =0;
						grd_Caption.CellAlignment=4;
						grd_Caption.set_TextMatrix(0,c,sArr[c]) ;
						grd_Caption.Row =1 ;
						grd_Caption.CellAlignment=4;
						grd_Caption.set_TextMatrix(1,c,sArr[c]) ;
						grd_Grid.Col =c;
						grd_Grid.Row =0;
						grd_Grid.CellAlignment=4;
						grd_Grid.set_TextMatrix(0,c,sArr[c]) ;
					}
					//					grd_Caption.FormatString =formatString;
					//					grd_Grid.FormatString =formatString;
					//grd_Caption.get_Cols(0)-2 ;
					//					}
					//					else
					//					{
					//						grd_Grid.FormatString=formatString; 
					//						grd_Caption.Visible =false;
					//					}
					grd_Grid.Col =1;
					grd_Grid.Row =1;

					SetColWidth(setcolWidth);

				}

			} 
		} 

		///<summary>
		/// 设置网格标题头1行格式
		/// </summary>
		/// 
		public string FormatString1
		{ 
			get {return formatString1;} 
			set 
			{
				formatString1 =value;
				if (formatString1==null || formatString1=="") {formatString1=formatString;}
				if (formatString==formatString1)
				{multiHead=false ;grd_Caption.Visible =false;} 
				else 
				{multiHead=true ;grd_Caption.Visible =true;}

				if (formatString!=formatString1)
				{
					//grd_Caption.FormatString =formatString1;
					string [] sArr=null ;
					sArr=formatString1.Split('|') ;
					int cs=sArr.Length;
					grd_Caption.set_MergeRow(0, true) ; grd_Caption.set_MergeRow(1, true);
					for (int c=0;c<cs;c++)
					{
						grd_Caption.set_MergeCol(c, true);
						grd_Caption.Col =c;
						grd_Caption.Row =1 ;
						grd_Caption.CellAlignment=4;
						grd_Caption.set_TextMatrix(1,c,sArr[c]) ;
					}
					SetColWidth(setcolWidth);
				}
			} 
		} 
		/// <summary>
		/// 设置列控制格式字符串
		/// </summary>
		public string ColCommanFormat
		{ 
			get {return colCommanFormat;} 
			set {colCommanFormat=value;}
		}

		/// 设置网格明细标题头格式
		/// </summary>
		public string DetailFormat
		{ 
			get {return detailFormat;} 
			set 
			{
				detailFormat =value;
				if (detailFormat!=null)
				{
					string [] sArr=null ;
					sArr=detailFormat.Split('|') ;
					int cs=sArr.Length;
					string t="";
					for (int i=0;i<cs;i++)
					{
						if (sArr[i].Trim()!="")
						{
							t=t+sArr[i].Trim().Substring(0,1)+"|";
						}
						else
						{
							t=t+"|";
						}
					}
					t=t.Substring(0,t.Length-1);
					grd_Grid.FormatString =t;
					for (int c=0;c<cs;c++)
					{
						grd_Grid.set_TextMatrix(0,c,sArr[c]) ;
						grd_Grid.set_ColWidth(c,0,grd_Caption.get_ColWidth(c,0));
						//						grd_Caption.set_TextMatrix(2,c,sArr[c]) ;

						//						switch (sArr[c].Substring(1,1) )
						//						{
						//							case "<":
						//								grd_Caption.CellAlignment=1;
						//								break;
						//							case ">":
						//								grd_Caption.CellAlignment=7;
						//								break;
						//							case "^":
						//								grd_Caption.CellAlignment=4;
						//								break;
						//						default:
						//								grd_Caption.CellAlignment=1;
						//								break;					
						//						
					}
				}
				grd_Grid.LeftCol = grd_Caption.LeftCol;
			}
		}	  
		/// <summary>
		/// 设置固定行列背景色
		/// </summary>
		public Color FixedBackColor
		{ 
			get {return fixedBackColor;} 
			set 
			{
				fixedBackColor=value;
				//				if (fixedBackColor!=null)
				//				{
				grd_Caption.BackColorFixed =fixedBackColor;
				grd_Grid.BackColorFixed =fixedBackColor;
				//				}
			} 
		}
		/// <summary>
		/// 设置单元格背景色
		/// </summary>
		public Color CellBackColor
		{ 
			get {return grd_Grid.CellBackColor;} 
				
			set { grd_Grid.CellBackColor=value;} 
		} 
		/// <summary>
		/// 设置单元格前景色
		/// </summary>
		public Color CellForeColor
		{ 
			get {return grd_Grid.CellForeColor;} 
				
			set 
			{ 
				grd_Grid.CellForeColor=value;
			} 
		}
		/// <summary>
		/// 设置网格前景色
		/// </summary>
		public new  Color  ForeColor
		{ 
			get {return grd_Grid.ForeColor;} 
				
			set 
			{ 
				grd_Grid.ForeColor=value;
			} 
		} 
		/// <summary>
		/// 设置网格前景色
		/// </summary>
		public new Color BackColor
		{ 
			get {return grd_Grid.BackColor;} 
				
			set 
			{ 
				grd_Grid.BackColor=value;
			} 
		} 

		/// <summary>
		/// 设置网格颜色
		/// </summary>
		public Color GridColor
		{ 
			get {return grd_Grid.GridColor;} 
				
			set 
			{ 
				grd_Grid.GridColor=value;
			} 
		} 

		/// <summary>
		/// 设置网格高亮颜色
		/// </summary>
		public Color BackColorSel
		{ 
			get {return grd_Grid.BackColorSel ;} 
				
			set 
			{ 
				grd_Grid.BackColorSel=value;
			} 
		} 

		/// <summary>
		/// 在单元格中是否接收回车调用tab键
		/// </summary>
		public bool EnterToTab 
		{ 
			get {return enterToTab;} 
				
			set { enterToTab=value;} 
		} 
		/// <summary>
		/// 是否有外边框(平面2D 还是3D)。
		/// </summary>
		public bool BorderStyle 
		{ 
			get {return borderStyle;} 
			set 
			{ 
				borderStyle=value;
				if (borderStyle==true)
				{
					grd_Caption.Appearance=MSHierarchicalFlexGridLib.AppearanceSettings.flex3D; 
					grd_Grid.Appearance=MSHierarchicalFlexGridLib.AppearanceSettings.flex3D; 
				}
				else
				{
					grd_Caption.Appearance=MSHierarchicalFlexGridLib.AppearanceSettings.flexFlat ; 
					grd_Grid.Appearance=MSHierarchicalFlexGridLib.AppearanceSettings.flexFlat ; 
				}
			} 
		} 
				
		/// <summary>
		/// 单元格边框类型。
		/// </summary>
		public bool GridStyle 
		{ 
			get {return gridStyle;} 
			set { gridStyle=value;} 
		} 
		/// <summary>
		/// 左边第一列边缘的X坐标
		/// </summary>
		public int GetLeftCol
		{ 
			get {return grd_Caption.LeftCol ;} 
		} 
		/// <summary>
		/// 左边第一列边缘的X坐标
		/// </summary>
		public int SetLeftCol
		{ 
			set	{grd_Caption.LeftCol=value;	} 
		} 
		/// <summary>
		/// 当前行。
		/// </summary>
		public int Row 
		{ 
			get {return row;} 
			set {row=value;grd_Grid.Row=row;} 
		} 
		/// <summary>
		///当前列。
		/// </summary>
		public int Col 
		{ 
			get {return col;} 
			set {col=value;grd_Grid.Col =col;grd_Caption.Col =col;} 
		} 
		/// <summary>
		/// 网格行数。
		/// </summary>
		public int Rows 
		{ 
			get {return rows;} 
			set
			{
				rows=value;
				grd_Grid.Rows=rows;
			} 
   
		} 
		/// <summary>
		/// 网格列数。
		/// </summary>
		public int Cols 
		{ 
			get {return cols;} 
			set 
			{
				cols=value;
				grd_Caption.set_Cols(0,cols);
				grd_Grid.set_Cols(0,cols);
				SetColWidth(0,500);
			} 
   
		} 
		/// <summary>
		/// 设定固定列行。
		/// </summary>
		public int FixedCols
		{
			get {return grd_Grid.FixedCols;} 
			set 
			{
				grd_Grid.FixedCols=value;
				grd_Caption.FixedCols=value;
			}
		}

		/// <summary>
		/// 滚动条。
		/// </summary>
		public string ScrollBars 
		{ 
			set 
			{
				switch (value)
				{
					case "None":// "0":
						grd_Grid.ScrollBars=MSHierarchicalFlexGridLib.ScrollBarsSettings.flexScrollBarNone  ;
						break;
					case "Both"://"1":
						grd_Grid.ScrollBars=MSHierarchicalFlexGridLib.ScrollBarsSettings.flexScrollBarBoth ;
						break;
					case "Horizontal"://"2":
						grd_Grid.ScrollBars=MSHierarchicalFlexGridLib.ScrollBarsSettings.flexScrollBarHorizontal ;
						break;
					case "Vertical":// "3":
						grd_Grid.ScrollBars=MSHierarchicalFlexGridLib.ScrollBarsSettings.flexScrollBarVertical ;
						break;
					default:
						grd_Grid.ScrollBars=MSHierarchicalFlexGridLib.ScrollBarsSettings.flexScrollBarBoth ;
						break;

				}

			} 
   
		} 
		
		/// <summary>
		/// 单元格列间距。
		/// </summary>
		public int CellColLength 
		{ 
			get {return cellColLength;} 
			set { cellColLength=value; 	} 
		}
		/// <summary>
		/// 单元格行间距。
		/// </summary>
		public int CellRowLength 
		{ 
			get {return cellRowLength;} 
			set	{cellRowLength=value; } 
		} 
		/// <summary>
		/// 单元格宽。
		/// </summary>
		public int CellWidth 
		{ 
			get {return cellWidth;} 
		} 
		/// <summary>
		/// 单元格高。
		/// </summary>
		public int CellHeight 
		{ 
			get {return cellHeight;} 
		} 	
		/// <summary>
		/// 单元格顶边距。
		/// </summary>
		public int CellTop 
		{ 
			get {return cellTop;} 
		} 
		/// <summary>
		/// 单元格左边距。
		/// </summary>
		public int CellLeft 
		{ 
			get {return cellLeft;} 
		} 
		/// <summary>
		/// 网格列宽值信息。
		/// </summary>
		public string SetCellWidth 
		{   
			get { return setcolWidth; } 
			set 
			{    
				//				if (SetCellWidth==null) 
				//				{
				////					setcolWidth="100"; 
				////					for (int i=1;i<cols;i++)
				////					{
				////						setcolWidth+=",100";
				////					}
				//				}

				setcolWidth=value;
				SetColWidth(setcolWidth);
			} 
		} 
		/// <summary>
		/// 单元格扩展信息。
		/// </summary>
		public string CellTag 
		{   
			get { return cellTag; } 
			set 
			{    
				if (value!=null) 
					cellTag=value; 
			} 
		} 
		/// <summary>
		/// 返回指定的单元格内容。
		/// </summary>

		public string GetCellText(int r,int c) 
		{

		{ return grd_Grid.get_TextMatrix(r,c);}

		}
		
		/// <summary>
		/// 返回指定列的控制命令或列的标题。
		/// </summary>
		/// <param name="c"></param>
		/// <returns></returns>
		public string GetCurColCommand(int c) 
		{
			string [] sArr=null ;
			sArr=colCommanFormat.Split('|') ;
		{ return sArr[c];}

		}
		#endregion 获得设置属性
		public MyFlexGrid()
		{
			// 该调用是 Windows.Forms 窗体设计器所必需的。
			InitializeComponent();

			// TODO: 在 InitComponent 调用后添加任何初始化

		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			this.grd_Caption.Dispose();
			this.grd_Grid.Dispose();
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );

		}

		#region 组件设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MyFlexGrid));
			this.grd_Grid = new AxMSHierarchicalFlexGridLib.AxMSHFlexGrid();
			this.txt_Cell = new System.Windows.Forms.TextBox();
			this.RigtMenu = new System.Windows.Forms.ContextMenu();
			this.Mnu_RowsAdd = new System.Windows.Forms.MenuItem();
			this.Mnu_AddRow = new System.Windows.Forms.MenuItem();
			this.Mnu_DelRow = new System.Windows.Forms.MenuItem();
			this.Mnu_GridClear = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.Mnu_ColHide = new System.Windows.Forms.MenuItem();
			this.Mnu_ColAllShow = new System.Windows.Forms.MenuItem();
			this.Mnu_ColWidth = new System.Windows.Forms.MenuItem();
			this.Mnu_ColWidthAuto = new System.Windows.Forms.MenuItem();
			this.Mnu_ColWidthAVG = new System.Windows.Forms.MenuItem();
			this.Mnu_ColSum = new System.Windows.Forms.MenuItem();
			this.menuItem11 = new System.Windows.Forms.MenuItem();
			this.Mnu_Copy = new System.Windows.Forms.MenuItem();
			this.Mnu_Cut = new System.Windows.Forms.MenuItem();
			this.Mnu_Paste = new System.Windows.Forms.MenuItem();
			this.Mnu_SaveAs = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.Mnu_ColASC = new System.Windows.Forms.MenuItem();
			this.Mnu_ColDesc = new System.Windows.Forms.MenuItem();
			this.grd_Caption = new AxMSHierarchicalFlexGridLib.AxMSHFlexGrid();
			this.chk_YesOrNo = new System.Windows.Forms.CheckBox();
			((System.ComponentModel.ISupportInitialize)(this.grd_Grid)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.grd_Caption)).BeginInit();
			this.SuspendLayout();
			// 
			// grd_Grid
			// 
			this.grd_Grid.DataSource = null;
			this.grd_Grid.Location = new System.Drawing.Point(0, 34);
			this.grd_Grid.Name = "grd_Grid";
			this.grd_Grid.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("grd_Grid.OcxState")));
			this.grd_Grid.Size = new System.Drawing.Size(354, 166);
			this.grd_Grid.TabIndex = 1;
			this.grd_Grid.ClickEvent += new System.EventHandler(this.grd_Grid_ClickEvent);
			this.grd_Grid.MouseMoveEvent += new AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseMoveEventHandler(this.grd_Grid_MouseMoveEvent);
			this.grd_Grid.KeyDownEvent += new AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_KeyDownEventHandler(this.grd_Grid_KeyDownEvent);
			this.grd_Grid.EnterCell += new System.EventHandler(this.grd_Grid_EnterCell);
			this.grd_Grid.Scroll += new System.EventHandler(this.grd_Grid_Scroll);
			this.grd_Grid.DblClick += new System.EventHandler(this.grd_Grid_DblClick);
			this.grd_Grid.MouseUpEvent += new AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseUpEventHandler(this.grd_Grid_MouseUpEvent);
			this.grd_Grid.MouseDownEvent += new AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseDownEventHandler(this.grd_Grid_MouseDownEvent);
			// 
			// txt_Cell
			// 
			this.txt_Cell.AutoSize = false;
			this.txt_Cell.BackColor = System.Drawing.Color.LightBlue;
			this.txt_Cell.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.txt_Cell.Location = new System.Drawing.Point(132, 246);
			this.txt_Cell.Name = "txt_Cell";
			this.txt_Cell.Size = new System.Drawing.Size(62, 18);
			this.txt_Cell.TabIndex = 2;
			this.txt_Cell.Text = "Null";
			this.txt_Cell.Visible = false;
			this.txt_Cell.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_Cell_KeyDown);
			this.txt_Cell.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txt_Cell_MouseDown);
			this.txt_Cell.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_Cell_KeyPress);
			this.txt_Cell.TextChanged += new System.EventHandler(this.txt_Cell_TextChanged);
			this.txt_Cell.DoubleClick += new System.EventHandler(this.txt_Cell_DoubleClick);
			this.txt_Cell.MouseUp += new System.Windows.Forms.MouseEventHandler(this.txt_Cell_MouseUp);
			this.txt_Cell.Leave += new System.EventHandler(this.txt_Cell_Leave);
			this.txt_Cell.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_Cell_KeyUp);
			this.txt_Cell.Enter += new System.EventHandler(this.txt_Cell_Enter);
			// 
			// RigtMenu
			// 
			this.RigtMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.Mnu_RowsAdd,
																					 this.Mnu_AddRow,
																					 this.Mnu_DelRow,
																					 this.Mnu_GridClear,
																					 this.menuItem5,
																					 this.Mnu_ColHide,
																					 this.Mnu_ColAllShow,
																					 this.Mnu_ColWidth,
																					 this.Mnu_ColWidthAuto,
																					 this.Mnu_ColWidthAVG,
																					 this.Mnu_ColSum,
																					 this.menuItem11,
																					 this.Mnu_Copy,
																					 this.Mnu_Cut,
																					 this.Mnu_Paste,
																					 this.Mnu_SaveAs,
																					 this.menuItem1,
																					 this.Mnu_ColASC,
																					 this.Mnu_ColDesc});
			// 
			// Mnu_RowsAdd
			// 
			this.Mnu_RowsAdd.Index = 0;
			this.Mnu_RowsAdd.Shortcut = System.Windows.Forms.Shortcut.Ctrl5;
			this.Mnu_RowsAdd.Text = "插入5行";
			this.Mnu_RowsAdd.Click += new System.EventHandler(this.Mnu_RowsAdd_Click);
			// 
			// Mnu_AddRow
			// 
			this.Mnu_AddRow.Index = 1;
			this.Mnu_AddRow.Shortcut = System.Windows.Forms.Shortcut.Ctrl1;
			this.Mnu_AddRow.Text = "插入一行";
			this.Mnu_AddRow.Click += new System.EventHandler(this.Mnu_AddRow_Click);
			// 
			// Mnu_DelRow
			// 
			this.Mnu_DelRow.Index = 2;
			this.Mnu_DelRow.Shortcut = System.Windows.Forms.Shortcut.CtrlDel;
			this.Mnu_DelRow.Text = "删除行";
			this.Mnu_DelRow.Click += new System.EventHandler(this.Mnu_DelRow_Click);
			// 
			// Mnu_GridClear
			// 
			this.Mnu_GridClear.Index = 3;
			this.Mnu_GridClear.Shortcut = System.Windows.Forms.Shortcut.Del;
			this.Mnu_GridClear.Text = "清除选定单元格的内容";
			this.Mnu_GridClear.Click += new System.EventHandler(this.Mnu_GridClear_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 4;
			this.menuItem5.Text = "-";
			// 
			// Mnu_ColHide
			// 
			this.Mnu_ColHide.Index = 5;
			this.Mnu_ColHide.Shortcut = System.Windows.Forms.Shortcut.CtrlH;
			this.Mnu_ColHide.Text = "隐藏选定列";
			this.Mnu_ColHide.Click += new System.EventHandler(this.Mnu_ColHide_Click);
			// 
			// Mnu_ColAllShow
			// 
			this.Mnu_ColAllShow.Index = 6;
			this.Mnu_ColAllShow.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
			this.Mnu_ColAllShow.Text = "显所有列";
			this.Mnu_ColAllShow.Click += new System.EventHandler(this.Mnu_ColAllShow_Click);
			// 
			// Mnu_ColWidth
			// 
			this.Mnu_ColWidth.Index = 7;
			this.Mnu_ColWidth.Text = "设置当前列宽";
			this.Mnu_ColWidth.Visible = false;
			// 
			// Mnu_ColWidthAuto
			// 
			this.Mnu_ColWidthAuto.Index = 8;
			this.Mnu_ColWidthAuto.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
			this.Mnu_ColWidthAuto.Text = "自动设置列宽";
			this.Mnu_ColWidthAuto.Click += new System.EventHandler(this.Mnu_ColWidthAuto_Click);
			// 
			// Mnu_ColWidthAVG
			// 
			this.Mnu_ColWidthAVG.Index = 9;
			this.Mnu_ColWidthAVG.Text = "平均列宽";
			this.Mnu_ColWidthAVG.Visible = false;
			// 
			// Mnu_ColSum
			// 
			this.Mnu_ColSum.Index = 10;
			this.Mnu_ColSum.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
			this.Mnu_ColSum.Text = "计算选定区域合计";
			this.Mnu_ColSum.Click += new System.EventHandler(this.Mnu_ColSum_Click);
			// 
			// menuItem11
			// 
			this.menuItem11.Index = 11;
			this.menuItem11.Text = "-";
			// 
			// Mnu_Copy
			// 
			this.Mnu_Copy.Index = 12;
			this.Mnu_Copy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
			this.Mnu_Copy.Text = "复制";
			this.Mnu_Copy.Click += new System.EventHandler(this.Mnu_Copy_Click);
			// 
			// Mnu_Cut
			// 
			this.Mnu_Cut.Index = 13;
			this.Mnu_Cut.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
			this.Mnu_Cut.Text = "剪切";
			this.Mnu_Cut.Click += new System.EventHandler(this.Mnu_Cut_Click);
			// 
			// Mnu_Paste
			// 
			this.Mnu_Paste.Index = 14;
			this.Mnu_Paste.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
			this.Mnu_Paste.Text = "粘贴";
			this.Mnu_Paste.Click += new System.EventHandler(this.Mnu_Paste_Click);
			// 
			// Mnu_SaveAs
			// 
			this.Mnu_SaveAs.Index = 15;
			this.Mnu_SaveAs.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.Mnu_SaveAs.Text = "导出到EXCEL...";
			this.Mnu_SaveAs.Click += new System.EventHandler(this.Mnu_SaveAs_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 16;
			this.menuItem1.Text = "-";
			// 
			// Mnu_ColASC
			// 
			this.Mnu_ColASC.Index = 17;
			this.Mnu_ColASC.Text = "升序";
			this.Mnu_ColASC.Click += new System.EventHandler(this.Mnu_ColASC_Click);
			// 
			// Mnu_ColDesc
			// 
			this.Mnu_ColDesc.Index = 18;
			this.Mnu_ColDesc.Text = "降序";
			this.Mnu_ColDesc.Click += new System.EventHandler(this.Mnu_ColDesc_Click);
			// 
			// grd_Caption
			// 
			this.grd_Caption.DataSource = null;
			this.grd_Caption.Location = new System.Drawing.Point(0, 0);
			this.grd_Caption.Name = "grd_Caption";
			this.grd_Caption.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("grd_Caption.OcxState")));
			this.grd_Caption.Size = new System.Drawing.Size(354, 54);
			this.grd_Caption.TabIndex = 3;
			this.grd_Caption.MouseMoveEvent += new AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseMoveEventHandler(this.grd_Caption_MouseMoveEvent);
			this.grd_Caption.Scroll += new System.EventHandler(this.grd_Caption_Scroll);
			this.grd_Caption.DblClick += new System.EventHandler(this.grd_Caption_DblClick);
			this.grd_Caption.MouseUpEvent += new AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseUpEventHandler(this.grd_Caption_MouseUpEvent);
			this.grd_Caption.MouseDownEvent += new AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseDownEventHandler(this.grd_Caption_MouseDownEvent);
			// 
			// chk_YesOrNo
			// 
			this.chk_YesOrNo.BackColor = System.Drawing.Color.Transparent;
			this.chk_YesOrNo.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.chk_YesOrNo.Location = new System.Drawing.Point(264, 142);
			this.chk_YesOrNo.Name = "chk_YesOrNo";
			this.chk_YesOrNo.Size = new System.Drawing.Size(76, 16);
			this.chk_YesOrNo.TabIndex = 4;
			this.chk_YesOrNo.Visible = false;
			this.chk_YesOrNo.Leave += new System.EventHandler(this.chk_YesOrNo_Leave);
			this.chk_YesOrNo.CheckedChanged += new System.EventHandler(this.chk_YesOrNo_CheckedChanged);
			// 
			// MyFlexGrid
			// 
			this.Controls.Add(this.chk_YesOrNo);
			this.Controls.Add(this.grd_Grid);
			this.Controls.Add(this.txt_Cell);
			this.Controls.Add(this.grd_Caption);
			this.Name = "MyFlexGrid";
			this.Size = new System.Drawing.Size(354, 200);
			this.Resize += new System.EventHandler(this.MyFlexGrid_Resize);
			this.Load += new System.EventHandler(this.MyFlexGrid_Load);
			((System.ComponentModel.ISupportInitialize)(this.grd_Grid)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.grd_Caption)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void MyFlexGrid_Load(object sender, System.EventArgs e)
		{
			grd_Grid.set_RowHeight(0,0) ;
			//			this.grd_Grid.Top=grd_Caption.get_RowHeight(0)+grd_Caption.get_RowHeight(1) ;
			this.grd_Caption.MousePointer=MSHierarchicalFlexGridLib.MousePointerSettings.flexCustom ;
			Menu.SetCaptionSource =grd_Caption;
			Menu.SetGridSource =grd_Grid;
//			nToolTip.SetToolTip(grd_Grid,"行列["+grd_Grid.MouseRow.ToString()+","+grd_Grid.MouseCol.ToString()+"]");
		}

		#region 方法定义

		/// <summary>
		/// 得到网格明细数组，不包括标题行
		/// </summary>
		/// <returns></returns>
		public string [,] GetDetailArrary()
		{
			string [,] ArrText=null;
			 ArrText=new string[rows-1,cols-1];
				for (int r=1;r<rows;r++)
				{
					for (int c=0;c<cols;c++)
					{
						ArrText[r-1,c]=grd_Grid.get_TextMatrix(r,c) ;
					}
				}
			return ArrText;

		}
		/// <summary>
		/// 得到网格数组，包括标题行
		/// </summary>
		/// <returns></returns>
		public string [,] GetGridArrary()
		{
			string [,] ArrText=null;
			if (multiHead==true)
			{
				//得到网格明细数组
				string [,] ArrCaption=GetCaptionArrary();
				ArrText=new string[rows+1,cols+1];
				for (int r=0;r<2;r++)
				{
					for (int c=0;c<cols;c++)
					{
						ArrText[r,c]=ArrCaption[r,c] ;
					}
				}

				for (int r=1;r<rows;r++)
				{
					for (int c=0;c<cols;c++)
					{
						ArrText[r+1,c]=grd_Grid.get_TextMatrix(r,c) ;
					}
				}
			}
			else
			{
				ArrText=new string[rows,cols];
				for (int r=0;r<rows;r++)
				{
					for (int c=0;c<cols;c++)
					{
						ArrText[r,c]=grd_Grid.get_TextMatrix(r,c);
					}
				}

			}
			return ArrText;

		}
		/// <summary>
		/// 得到网格标题数组
		/// </summary>
		/// <returns></returns>
		public string [,] GetCaptionArrary()
		{
			string [,] ArrText=null;
			if (multiHead==true)
			{
				ArrText=new string[2,cols];

				for (int r=0;r<2;r++)
				{
					for (int c=0;c<cols;c++)
					{
						ArrText[r,c]=grd_Caption.get_TextMatrix(r,c);
					}
				}
			}
			else
			{
				 ArrText=new string[1,cols];

				for (int c=0;c<cols;c++)
				{
					ArrText[0,c]=grd_Grid.get_TextMatrix(0,c);
				}
			}
			return ArrText;

		}		
		
		/// <summary>
		/// 返回当前单元格TextBox对象。
		/// </summary>	
		public System.Windows.Forms.TextBox GetCell()
		{
			return txt_Cell;
		}
		/// <summary>
		/// 写入指定的单元格。
		/// </summary>

		public void SetCellText(int r,int c,string strCell) 
		{
			try
			{
				grd_Grid.set_TextMatrix(r,c,strCell);
				if (r==0)
				{
					grd_Grid.Col =c;
					grd_Grid.Row =r ;
					grd_Grid.CellAlignment=4;
				}

			}
			catch(System.Exception ex)
			{			                
				System.Windows.Forms.MessageBox.Show( ex.Message,"提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);

			}	
		
		}
		/// <summary>
		/// 设置列宽:strColWidth 是字符串参数(例如:"100,1300,4567,900")
		/// </summary>

		public void SetColWidth(string strColWidth) 
		{
			if (strColWidth=="" || strColWidth==null) {return;}
			string [] sArr=null ;

			sArr=strColWidth.Split(',') ;
			int cs=sArr.Length;
			if (cs>cols)  {cs=cols;}
			for (int i=0;i<cs;i++)
			{
				grd_Caption.set_ColWidth(i,0,int.Parse(sArr[i]));
				grd_Grid.set_ColWidth(i,0,int.Parse(sArr[i]));
					
			}
			grd_Grid.LeftCol = grd_Caption.LeftCol;

			sArr=null ;
		}
		/// <summary>
		/// 设置0列行号
		/// </summary>
		public void SetRowNo() 
		{
			int cs=grd_Grid.Rows;
			for (int i=1;i<cs;i++)
			{
				grd_Grid.set_TextMatrix(i,0,i.ToString() );
			}
		}
		/// <summary>
		/// 设置网格一列宽。
		/// </summary>

		public void SetColWidth(int c,int colWidth) 
		{
			grd_Caption.set_ColWidth(c,0,colWidth);
			grd_Grid.set_ColWidth(c,0,colWidth);
		}

		/// <summary>
		/// 得到网格当前列宽设置字符串 返回如:"120,120,100,110,119,0,111"
		/// </summary>

		public string GetColWidth()
		{
			int n=grd_Caption.get_ColWidth(0,0);
			setcolWidth=n.ToString();
			for (int i=1;i<cols;i++)
			{
				n=grd_Caption.get_ColWidth(i,0);
				if (n==-1){n=990;}
				setcolWidth=setcolWidth+","+n.ToString() ;
			}

			return setcolWidth;
		}
		/// <summary>
		/// 得到任意一列的列宽
		/// </summary>
		/// <param name="c"></param>
		/// <returns></returns>
		public int ColWidth(int c)
		{
			int n=grd_Caption.get_ColWidth(c,0);
			return n;
		}
		/// <summary>
		/// 设置行高。
		/// </summary>

		public void SetRowHeight(int r,int rowHeight) 
		{
			grd_Grid.set_RowHeight(r,rowHeight) ;

		}
		/// <summary>
		/// 得到行高。
		/// </summary>

		public int GetRowHeight(int r) 
		{
			return grd_Grid.get_RowHeight(r);

		}
		/// <summary>
		/// 插入一行。
		/// </summary>
		public void RowInsert(string item,int index) 
		{
			grd_Grid.AddItem(item,index);
			grd_Grid.set_RowHeight(index, grd_Grid.get_RowHeight(1));
			rows=rows+1;
			grd_Grid.Row=index;
			row=index;

		}
		/// <summary>
		/// 插入多行。
		/// </summary>
		public void RowInsert(string item,int index,int rs) 
		{
			for (int r=0;r<rs;r++)
			{
				grd_Grid.AddItem(item,index);
				grd_Grid.set_RowHeight(index, grd_Grid.get_RowHeight(1));
				rows=rows+1;
			}
			grd_Grid.Row=index;
			row=index;

		}

		/// <summary>
		/// 删除一行。
		/// </summary>
		public void RowRemove(int index) 
		{
			grd_Grid.RemoveItem(index);
			rows=rows-1;
			GridRefresh();

		}
		/// <summary>
		/// 清除网格内容（开始行1）。
		/// </summary>
		public void Clear() 
		{
			int rs=grd_Grid.Rows;
			int cs=grd_Grid.get_Cols(0);
			for (int r=1;r<rs;r++)
			{
				for (int c=0;c<cs;c++)
				{
					grd_Grid.set_TextMatrix(r,c,"");
					grd_Grid.set_TextMatrix(r,0,r.ToString());
				}
			}
			GridRefresh();
		}
		/// <summary>
		/// 自动设置列宽
		/// </summary>
		public void ColWidthAuto() 
		{
			Menu.ColWidthAuto();
		}
		/// <summary>
		/// 选中区域剪切
		/// </summary>
		public void AreaCut()
		{
			Menu.GridAreaCopy(true);
		}
		/// <summary>
		/// 选中区域拷贝
		/// </summary>
		public void AreaCopy()
		{
			Menu.GridAreaCopy(false);
		}
		/// <summary>
		/// 选中区域粘贴
		/// </summary>
		public void AreaPaste()
		{
			Menu.GridAreaPaste();
		}
//
//
//'改变表格中指定行列范围内的颜色
//'参数：flexTemp 表名，StartRow 开始行，StartCol 开始列，EndRow 结束行，EndCol 结束列
//public Sub SetFlexGridColor(flexTemp As Object, Optional ByVal StartRow As Integer, _
//Optional ByVal StartCol As Integer, Optional ByVal EndRow As Integer, Optional ByVal EndCol As Integer, _
//Optional ByVal Color As Long = &HFFFF80)
//Dim OldRow As Integer, oldCol As Integer
//
//With flexTemp
//.GridColor = &H808000
//If .Rows = 0 Then Exit Sub   '2001-3-30 修改  （.Rows = 1）
//If Color = &HFFFF80 Then Color = RGB(203, 229, 229)
//
//OldRow = .Row
//oldCol = .Col
//If EndCol = 0 Then EndCol = StartCol
//.FillStyle = flexFillRepeat
//.Col = StartCol
//.Row = StartRow
//.ColSel = EndCol
//.RowSel = EndRow
//.CellBackColor = Color
//.FillStyle = flexFillSingle
//.Row = OldRow
//.Col = oldCol
//End With
//End Sub
//'改变表格中指定行列范围内的颜色
//'参数：flexTemp 表名
		public void SetAreaBackColor(Color p_Color)
		{
				if (grd_Grid.Rows == 0) return ;
				if (grd_Grid.RowSel == 0) return ;
				if (grd_Grid.ColSel == 0) return ;
				grd_Grid.FillStyle =MSHierarchicalFlexGridLib.FillStyleSettings.flexFillRepeat;
				grd_Grid.CellBackColor = p_Color;
				grd_Grid.FillStyle = MSHierarchicalFlexGridLib.FillStyleSettings.flexFillSingle;
		}
		public void SetAreaForeColor(Color p_Color)
		{
			if (grd_Grid.Rows == 0) return ;
			if (grd_Grid.RowSel == 0) return ;
			if (grd_Grid.ColSel == 0) return ;

			grd_Grid.FillStyle =MSHierarchicalFlexGridLib.FillStyleSettings.flexFillRepeat;
			grd_Grid.CellForeColor  = p_Color;
			grd_Grid.FillStyle = MSHierarchicalFlexGridLib.FillStyleSettings.flexFillSingle;
		}
		/// <summary>
		/// 填充列标题。
		/// </summary>

		//		public void FullColCaption(FlexGrid p_Grid,string strformat) 
		//		{
		//			if (formatString1!=null)
		//			{
		//				//					grd_Caption.FormatString =formatString1;
		//				int cs=5 ;
		//				//					cs=cols;
		//				string [] sArr=null ;
		//				sArr=formatString1.Split('|') ;
		//				cs=sArr.Length;
		//				p_Grid.set_MergeRow(0, true) ; p_Grid.set_MergeRow(1, true);
		//				for (int c=0;c<cs;c++)
		//				{
		//					p_Grid.set_MergeCol(c, true);
		//					p_Grid.Col =c;
		//
		//					p_Grid.Row =0;
		//					p_Grid.CellAlignment=4;
		//					grd_Caption.Row =1 ;
		//					grd_Caption.CellAlignment=4;
		//					grd_Caption.set_TextMatrix(1,c,sArr[c]) ;
		//
		//								
		//				}
		//
		//			}
		//
		//		}

		#endregion 
		private void MyFlexGrid_Resize(object sender, System.EventArgs e)
		{
			try
			{
				if (multiHead==true)
				{
					grd_Caption.Visible =true;
					this.grd_Caption.Top=0;
					this.grd_Caption.Left=0;
					this.grd_Caption.Width =this.Width ;
					this.grd_Grid.Left=0;
					this.grd_Grid.Width =this.Width ;
					this.grd_Grid.Height  =this.Height -grd_Grid.Top  ;
					double t=(grd_Caption.get_RowHeight(0)+grd_Caption.get_RowHeight(1))/15;

					this.grd_Grid.Top=int.Parse(t.ToString())-1 ;
					grd_Grid.set_RowHeight(0,0);
				}
				else
				{
					grd_Caption.Visible =false;
					this.grd_Grid.Left=0;
					this.grd_Grid.Width =this.Width ;
					this.grd_Grid.Top=0;
					this.grd_Grid.Height  =this.Height;
					grd_Grid.set_RowHeight(0,grd_Caption.get_RowHeight(0)+150);

				}
			}
			catch
			{
				return;
			}

		}

		
		private void grd_Caption_MouseUpEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseUpEvent e)
		{
			double w;
			double c;
			int cw;
			txt_Cell.Visible =false;
			chk_YesOrNo.Visible =false;
			grd_Caption.Col = grd_Caption.MouseCol;
			grd_Grid.Col = grd_Caption.Col;
			
			if (e.button == 2 )
			{
				grd_Caption.Col = grd_Caption.MouseCol;
				grd_Caption.Row =2;
				c=grd_Caption.CellWidth - 200;
				if(c < 1)
				{w=0;}
				else
				{w=c;}
			}
			else
			{
				w=grd_Caption.CellWidth + 200;
			}
			cw=int.Parse( w.ToString());
			grd_Caption.set_ColWidth(grd_Caption.Col,0,cw );
			grd_Grid.set_ColWidth(grd_Caption.Col, 0 ,cw);
			grd_Grid.LeftCol = grd_Caption.LeftCol;
			grd_Grid.Row =1;
			grd_Grid.RowSel=grd_Grid.Rows-1 ;

		}

		private void grd_Caption_ClickEvent(object sender, System.EventArgs e)
		{
			grd_Caption.Col = grd_Caption.MouseCol;
			//			grd_Caption.Row=0;
			grd_Grid.Col = grd_Caption.MouseCol ;
			grd_Grid.ColSel =grd_Grid.Col;
			grd_Grid.Row =1;
			grd_Grid.RowSel=grd_Grid.Rows-1 ;
			grd_Caption.Col = grd_Caption.MouseCol;
		}
		#region 菜单单击事件过程
		// 菜单单击事件过程
		private void Mnu_AddRow_Click(object sender, System.EventArgs e)
		{
			Menu.RowAdd();
			GridRefresh();

		}

		private void Mnu_DelRow_Click(object sender, System.EventArgs e)
		{
			Menu.RowDel();
			GridRefresh();
		}

		private void Mnu_ColASC_Click(object sender, System.EventArgs e)
		{
			Menu.ColSort(1);
		}

		private void Mnu_ColDesc_Click(object sender, System.EventArgs e)
		{
			Menu.ColSort(2);
		}

		private void Mnu_ColHide_Click(object sender, System.EventArgs e)
		{
			Menu.ColHide();//(grd_Grid,grd_Caption);
		}
		private void Mnu_RowsAdd_Click(object sender, System.EventArgs e)
		{
			Menu.RowsAdd();
			GridRefresh();
		}

		private void Mnu_Copy_Click(object sender, System.EventArgs e)
		{
			Menu.GridAreaCopy(false);
		}

		private void Mnu_GridClear_Click(object sender, System.EventArgs e)
		{
			Menu.GridClear();
		}

		private void Mnu_ColAllShow_Click(object sender, System.EventArgs e)
		{
			 Menu.ColAllShow();
		}

		private void Mnu_ColWidth_Click(object sender, System.EventArgs e)
		{
			//			Menu.GridAreaCopy(false);

		}

		private void Mnu_ColWidthAuto_Click(object sender, System.EventArgs e)
		{
			Menu.ColWidthAuto();

		}

		private void Mnu_ColWidthAVG_Click(object sender, System.EventArgs e)
		{
						Menu.ColWidthAvg();

		}

		private void Mnu_ColSum_Click(object sender, System.EventArgs e)
		{
				Menu.ColRowSum();

		}

		private void Mnu_Cut_Click(object sender, System.EventArgs e)
		{
						Menu.GridAreaCopy(true);

		}

		private void Mnu_Paste_Click(object sender, System.EventArgs e)
		{
					Menu.GridAreaPaste();
		}

		private void Mnu_SaveAs_Click(object sender, System.EventArgs e)
		{
			Menu.GridSaveAs();

		}
		#endregion 

		#region 定义事件过程。

		/// <summary>
		/// 定义事件过程。
		/// </summary>	
	

		/// <summary>
		/// 单元格鼠标按下事件过程。
		/// </summary>	
		//		public void Cell_MouseDown ( object sender , System.Windows.Forms.MouseEventArgs e )
		//		{
		//			if (sender.GetType()== typeof(Label))
		//			{
		//				Label control = (Label) sender ;
		//			}
		//			else 
		//			{
		//				TextBox control = (TextBox) sender ; 
		//
		//			}
		//			if (NewCell_MouseDown!=null)	
		//			{
		//				NewCell_MouseDown(sender, e );
		//			}
		//		}
		////		/// <summary>
		////		/// 单元格按键事件过程。
		////		/// </summary>	
		////		private void Cell_KeyPress ( object sender , System.Windows.Forms.KeyPressEventArgs e )
		////		{
		////			if (sender.GetType()== typeof(Label))
		////			{
		////				Label control = (Label) sender ;
		////			}
		////			else 
		////			{
		////				TextBox control = (TextBox) sender ; 
		////
		////			}
		////			
		////			//			GetCellAttribute(sender);
		////			if (NewCell_KeyPress!=null)	
		////			{
		////				NewCell_KeyPress(sender, e );
		////			}
		////			if (e.KeyChar == (char)13 && EnterToTab==true) 
		////			{			SendKeys.Send("{TAB}") ;}
		////
		////			//				MessageBox.Show ( control.Text + "被按动了！");
		////		}
		
		
		/// <summary>
		///单元格焦点离开事件过程。
		/// </summary>	
		private void txt_Cell_Leave(object sender, System.EventArgs e)
		{
			//			//出箱
			//			TextBox control = ( TextBox ) sender ; 
			//			//设定按钮的背景色
			//			control.BackColor = Color.SkyBlue ;
			txt_Cell.Visible =false;
			chk_YesOrNo.Visible =false;
			grd_Grid.Focus(); 
			if (Cell_Leave!=null)	
			{
				Cell_Leave(sender, e );
			}

		}
		
		
		private void txt_Cell_DoubleClick(object sender, System.EventArgs e)
		{
			if (Cell_DoubleClick!=null)	
			{
				Cell_DoubleClick(sender, e );
			}
			txt_Cell.Visible =false;
			chk_YesOrNo.Visible =false;
		}

		private void txt_Cell_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (Cell_KeyUp!=null)	
			{
				Cell_KeyUp(sender, e );
			}
		}
		private void txt_Cell_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (Cell_MouseDown!=null)	
			{
				Cell_MouseDown(sender, e );
			}
		}
		private void txt_Cell_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (Cell_KeyDown!=null)	
			{
				Cell_KeyDown(sender, e );
			}
		}

		private void grd_Caption_Scroll(object sender, System.EventArgs e)
		{
			grd_Grid.LeftCol = grd_Caption.LeftCol;
			txt_Cell.Visible =false;
			chk_YesOrNo.Visible =false;
		}

		private void grd_Grid_Scroll(object sender, System.EventArgs e)
		{
			grd_Caption.LeftCol =grd_Grid.LeftCol; 
			txt_Cell.Visible =false;
			chk_YesOrNo.Visible =false;
			if (Scroll!=null)	
			{
				Scroll(sender, e );
			}
			
		}
		private void grd_Grid_MouseDownEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseDownEvent e)
		{
//						if (Row_MouseDown!=null)	
//						{
//							Row_MouseDown(sender, e );
//						}
			nToolTip.SetToolTip(grd_Grid,"行列["+grd_Grid.MouseRow.ToString()+","+grd_Grid.MouseCol.ToString()+"]");		


		}
		/// <summary>
		/// 单元格单击事件过程。protected virtual void 
		/// </summary>	
		protected virtual void grd_Grid_ClickEvent(object sender, System.EventArgs e)
		{
			col=grd_Grid.Col ;
			row=grd_Grid.Row ; 
			ActiveCol=col ;
			ActiveRow=row ;
			if (Grid_Click!=null)	
			{
				Grid_Click(sender, e );
			}
			if (grd_Grid.Col !=grd_Grid.ColSel || grd_Grid.Row != grd_Grid.RowSel) 
			{return;}//多选,隐藏输入文本框
			ShowText();

			if (Row_Click!=null)	
			{
				Row_Click(sender, e );
			}
//            //判断是否是只读或是列锁定
//			if (onlyRead==true || colLocked==true)
//			{
//				txt_Cell.Visible =false;
//			}

		}

		/// <summary>
		/// 刷新
		/// </summary>	
		public void GridRefresh()
		{
			cols=grd_Grid.get_Cols(0);
			rows=grd_Grid.Rows;
			ActiveCol=1 ;
			ActiveRow=1 ; 
			col=1;
			row=1 ; 
			grd_Grid.Col=col ;
			grd_Grid.Row=row ; 
			txt_Cell.Visible =false;
			chk_YesOrNo.Visible =false;
		}


		/// <summary>
		/// 设置单元格输入焦点。
		/// </summary>	
		public void SetCellFocus(int r,int c)
		{
			if (r>grd_Grid.Rows-1){r=grd_Grid.Rows-1;}
			if (c>cols ){c=cols-1;}
			txt_Cell.Visible =false;
			chk_YesOrNo.Visible =false;
			ActiveCol=c ;
			ActiveRow=r ; 
			col=c ;
			row=r ; 
			grd_Grid.Col=col ;
			grd_Grid.Row=row ; 

			ShowText();			
		}

	    public void ShowText()
		{
			if (onlyRead==false)
			{
				if (colLocked==true) return ;
				//				int piCurrentLeft = 0;
				int piCurrentWidth = 0;
				//需要将其转化为像素，1440 缇 = 1 英寸 像素和厘米的大致比例是：40：1
				//在Windows 2000下，屏幕显示器设置为800*600像素，显示器的尺寸为15英寸（这个尺寸为屏幕对角线的长度），所以推断出显示器的长宽尺寸为12*9英寸(长宽比为4：3)，每英寸相当于25.4毫米，所以每个像素相当于12*25.4*10/800个0.1毫米，这个结果就是我们要得到的转换系数scale。需要提醒的一点是，由于逻辑坐标的Y轴以向上为正，所以在对Y轴坐标进行换算时，需要再乘上个-1。
				//			Screen.GetWorkingArea PrimaryScreen.WorkingArea; 
				//				grd_Grid.Col=col ;
				//				piCurrentLeft = grd_Grid.get_ColPos(grd_Grid.ColSel);
				//				piCurrentWidth = grd_Grid.get_ColWidth(grd_Grid.ColSel,0);
				piCurrentWidth = grd_Grid.CellWidth;
				cellWidth=piCurrentWidth/15;//grd_Grid.CellWidth  ; 
				cellLeft=(grd_Grid.Left +grd_Grid.CellLeft)/15;//grd_Grid.CellLeft  ; 
				cellHeight=grd_Grid.CellHeight/15  ; 

				double t;
				int A=0;
				t=grd_Grid.Top+grd_Grid.CellTop/15;//

				if (borderStyle==true) {A=1;} //如果是3D边框则减
				switch  (inputType)
				{
					case eInputType.TextBox :
						txt_Cell.Text=grd_Grid.get_TextMatrix(ActiveRow,ActiveCol);
						txt_Cell.Width =int.Parse(cellWidth.ToString());
						txt_Cell.Height =int.Parse(cellHeight.ToString());
						txt_Cell.Left  =int.Parse(cellLeft.ToString())-A;
						txt_Cell.Top   =int.Parse(t.ToString())-A ;
						txt_Cell.Visible =true;
						txt_Cell.BringToFront() ;
						cellTop=txt_Cell.Top ;
						txt_Cell.Focus() ;
						break;
					case eInputType.CheckBox :
						if (grd_Grid.get_TextMatrix(ActiveRow,ActiveCol)=="√") 
						{
							chk_YesOrNo.Checked=false;
							grd_Grid.set_TextMatrix(ActiveRow,ActiveCol,"");
						}
						else 
						{
							chk_YesOrNo.Checked=true;
							grd_Grid.set_TextMatrix(ActiveRow,ActiveCol,"√");
						}

						chk_YesOrNo.Width =int.Parse(cellWidth.ToString());
						chk_YesOrNo.Height =int.Parse(cellHeight.ToString());
						chk_YesOrNo.Left  =int.Parse(cellLeft.ToString())-A;
						chk_YesOrNo.Top   =int.Parse(t.ToString())-A ;
						chk_YesOrNo.Visible =true;
						chk_YesOrNo.BringToFront() ;
						cellTop=chk_YesOrNo.Top ;
						chk_YesOrNo.Focus() ;
						break;
					case eInputType.None:
						chk_YesOrNo.Visible =false;
						txt_Cell.Visible =false;
						break;
					default:
						
						break;	
       
				}

			}
		}

		private void grd_Grid_MouseUpEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseUpEvent e)
		{
			if (e.button ==2)
			{
				this.ContextMenu= this.RigtMenu ;
			}
		}
		private void txt_Cell_TextChanged(object sender, System.EventArgs e)
		{
			grd_Grid.set_TextMatrix(ActiveRow,ActiveCol,txt_Cell.Text);

			if (Cell_TextChanged!=null)	
			{
				Cell_TextChanged(sender, e );
			}

		}

		private void txt_Cell_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			//			if (e.KeyChar == (char)13) 
			//			{	
			//				SendKeys.Send("{TAB}") ;
			//			}
			//			if (e.KeyChar == (char)13) 
			//			{	
			//				SetCellFocus(grd_Grid.Row,grd_Grid.Col+1);
			//			}


			if (Cell_KeyPress!=null)	
			{
				Cell_KeyPress(sender, e );
			}

		}
		
		private void grd_Grid_DblClick(object sender, System.EventArgs e)
		{
			if (Row_DoubleClick!=null)
			{
				Row_DoubleClick(sender, e );
			}
		}
		/// <summary>
		/// 单元格得到焦点事件过程。
		/// </summary>	
		private void txt_Cell_Enter(object sender, System.EventArgs e)
		{
			if (Cell_Enter!=null)	
			{
				Cell_Enter(sender, e );
			}
			if (Cell_Click!=null)	
			{
				//				TextBox control = ( TextBox ) sender ; 
				Cell_Click(sender, e );
			}
		}

		private void txt_Cell_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (Cell_MouseUp!=null)	
			{
				Cell_MouseUp(sender, e );
			}
		
		}

		private void grd_Grid_KeyPressEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_KeyPressEvent e)
		{
			//			if (Row_KeyPress!=null)	
			//			{
			//				Row_KeyPress(sender, e );
			//			}

		}
		private void txt_Cell_MouseEnter(object sender, System.EventArgs e)
		{
			if (Cell_MouseEnter!=null)	
			{
				Cell_MouseEnter(sender, e );
			}
		}

		private void txt_Cell_MouseLeave(object sender, System.EventArgs e)
		{
			if (Cell_MouseLeave!=null)	
			{
				Cell_MouseLeave(sender, e );
			}
		}



		private void grd_Grid_EnterCell(object sender, System.EventArgs e)
		{
			txt_Cell.Text=grd_Grid.get_TextMatrix(ActiveRow,ActiveCol);

		}

		private void grd_Grid_MouseMoveEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseMoveEvent e)
		{

			if (e.button==1 && e.shift==1) 
			{
				CopyGridCellTextToSelCol();
			}

		}

		#endregion 

	/// <summary>
	/// 		CopyGridCellTextToSelCol拷贝网格单元格文本到选定列
	///	表格实现Excel拖拉复制数据功能,可在GridEdit_MouseMove事件或GridEdit_MouseUp事件中调用，但前者可以在拖拉过程中直接看到效果，后者拖完后可以看到
	/// </summary>

		public void CopyGridCellTextToSelCol()
		{
			int lngIndex;
			string txtSelText= grd_Grid.get_TextMatrix(grd_Grid.Row, grd_Grid.Col);
			if (grd_Grid.Col == grd_Grid.ColSel)   //'往纵向拖          '1  同时注释掉 1\2\3\4 时,则 横\纵同时拖
			{
				if (grd_Grid.Row <= grd_Grid.RowSel)
				{
					for( lngIndex = grd_Grid.Row;lngIndex<grd_Grid.RowSel+1;lngIndex++)
						//		'列不变
					{
						grd_Grid.set_TextMatrix(lngIndex, grd_Grid.Col,txtSelText) ;
					}
				}
				else
				{
					for (lngIndex = grd_Grid.RowSel;lngIndex< grd_Grid.Row+1;lngIndex++)

					{
						grd_Grid.set_TextMatrix(lngIndex, grd_Grid.Col,txtSelText) ;
					}
				}
			}
			if (grd_Grid.Row == grd_Grid.RowSel)   // '往横向拖        //  '3
			{
				if (grd_Grid.Col <= grd_Grid.ColSel)
				{
					for(lngIndex = grd_Grid.Col;lngIndex< grd_Grid.ColSel+1;lngIndex++)//		'行不变
					{
						grd_Grid.set_TextMatrix(grd_Grid.Row,lngIndex,txtSelText);
					}
				}
				else
				{
					for (lngIndex = grd_Grid.ColSel;lngIndex< grd_Grid.Col+1;lngIndex++)
					{
						grd_Grid.set_TextMatrix(grd_Grid.Row,lngIndex,txtSelText) ;
					}
				}
			}                   
		}

		private void grd_Caption_MouseMoveEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseMoveEvent e)
		{

		}

		private void grd_Caption_MouseDownEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_MouseDownEvent e)
		{
            grd_Caption.Col=grd_Caption.MouseCol ;
			nToolTip.SetToolTip(grd_Caption,"列["+grd_Caption.MouseCol.ToString()+"] 单击左键加宽,右键减宽");


		}
		/// <summary>
		/// 重写鼠标滚动事件
		/// </summary>
		/// <param name="e"></param>

		protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
		{
					txt_Cell.Visible =false;
			        chk_YesOrNo.Visible =false;
			switch (e.Delta)
			{
				case -360:
					this.Text ="-360";
					break;
				case -240:

					break;
				case -120:
					if (row<=1) return; 
						row=row-1;
						grd_Grid.Row=row;

//					grd_Grid.set_RowPosition(grd_Grid.Row,grd_Grid.Row);
////				System.Windows.Forms.ScrollEventType s= System.Windows.Forms.ScrollEventType;
//					grd_Grid.ScrollBars;
//					MSHierarchicalFlexGridLib.
//						e.Button;
//					grd_Grid.get_RowPos=grd_Grid.get_RowPos+grd_Grid.CellHeight ;

					break;
				case 360:
					this.Text ="360";
					break;
				case 240:
					this.Text ="240";
					break;
				case 120:
					if (row>=rows-1) return;
						row=row+1;
						grd_Grid.Row=row;
//					pos=grd_Grid.get_RowPos(grd_Grid.Row);
//					grd_Grid.set_RowPosition(
//					grd_Grid.set_RowPosition(grd_Grid.Row,grd_Grid.Row);//=grd_Grid.get_RowPos(grd_Grid.Row)-grd_Grid.CellHeight ;
					break;
				default:

					break;
			
			
			
			}
		
		}

		private void grd_Caption_DblClick(object sender, System.EventArgs e)
		{
		
		}

		private void grd_Grid_KeyDownEvent(object sender, AxMSHierarchicalFlexGridLib.DMSHFlexGridEvents_KeyDownEvent e)
		{
			 
		}


		/// <summary>
		/// 设置0列行号
		/// </summary>
		public void GetAttrib() 
		{
//			//MSHierarchicalFlexGrid网格常用的属性
//			grd_Grid.AddItem;
//			grd_Grid.BackColor ;
//			grd_Grid.BackColorBkg ;
//			grd_Grid.BackColorFixed ;
//			grd_Grid.BackColorSel ;
//			grd_Grid.BorderStyle ;
//			grd_Grid.CellAlignment ;
//			grd_Grid.CellAlignment ;
//			grd_Grid.CellBackColor ;
//			grd_Grid.CellFontBold ;
//			grd_Grid.CellFontItalic ;
//			grd_Grid.CellFontName ;
//			grd_Grid.CellFontSize ;
//			grd_Grid.CellFontStrikeThrough ;
//			grd_Grid.CellFontUnderline ;
//			grd_Grid.CellFontWidth ;
//			grd_Grid.CellForeColor ;
//			grd_Grid.CellHeight ;
//			grd_Grid.CellLeft ;
//			grd_Grid.CellPicture ;
//			grd_Grid.CellPictureAlignment ;
//			grd_Grid.CellTextStyle ;
//			grd_Grid.CellTop ;
//			grd_Grid.CellType ;
//			grd_Grid.CellWidth ;
//			grd_Grid.Clear();
//			grd_Grid.Col ;
//			grd_Grid.get_ColPos ;
//			grd_Grid.ColSel ;
//			grd_Grid.FixedCols ;
//			grd_Grid.FixedRows ;
//			grd_Grid.Focus ;
//			grd_Grid.Font ;
//			grd_Grid.FontBold ;
//			grd_Grid.FontItalic ;
//			grd_Grid.FontName ;
//			grd_Grid.FontSize ;
//			grd_Grid.FontStrikeThrough ;
//			grd_Grid.FontUnderline ;
//			grd_Grid.FontWidth ;
//			grd_Grid.ForeColor ;
//			grd_Grid.Height ;
//			grd_Grid.Left ;
//			grd_Grid.Picture ;
//			grd_Grid.TextStyle ;
//			grd_Grid.Top ;
//			grd_Grid.TextStyle ;
//			grd_Grid.Width ;
//			grd_Grid.Height ;
//			grd_Grid.ForeColorFixed ;
//			grd_Grid.ForeColorSel ;
//			grd_Grid.FormatString ;
//			grd_Grid.ForeColor ;
//			grd_Grid.FontWidthFixed ;
//			grd_Grid.LeftCol ;
//			grd_Grid.Location ;
//			grd_Grid.MergeCells ;
//			grd_Grid.MouseCol ;
//			grd_Grid.MousePointer ;
//			grd_Grid.MouseRow ;
//			grd_Grid.Parent ;
//			grd_Grid.PictureType ;
//			grd_Grid.Recordset ;
//			grd_Grid.Row ;
//			grd_Grid.Rows ;
//			grd_Grid.RowSel ;
//			grd_Grid.Rowset ;
//			grd_Grid.RowHeightMin;
//
//			grd_Grid.ScrollBars ;
//			grd_Grid.ScrollTrack ;
//			grd_Grid.Tag ;
//			grd_Grid.Text ;
//			grd_Grid.TextStyle ;
//			grd_Grid.TextStyleFixed ;
//			grd_Grid.WordWrap ;
//			//网格常用方法 及 事件
//			grd_Grid.set_ColAlignment;
//			grd_Grid.set_ColAlignmentFixed ;
//			grd_Grid.set_ColAlignmentHeader ;
//			grd_Grid.set_ColData ;
//			grd_Grid.set_ColHeader ;
//			grd_Grid.set_ColHeaderCaption ;
//			grd_Grid.set_ColPosition ;
//			grd_Grid.set_Cols ;
//			grd_Grid.set_ColWidth ;
//			grd_Grid.set_FixedAlignment ;
//			grd_Grid.set_FontHeader ;
//			grd_Grid.set_FontWidthHeader ;
//			grd_Grid.set_ForeColorBand ;
//			grd_Grid.set_GridColorBand ;
//			grd_Grid.set_TextArray ;
//			grd_Grid.set_TextMatrix ;
//			grd_Grid.set_TextStyleHeader ;
//			grd_Grid.Sort;
//			grd_Grid.get_Cols;
//			grd_Grid.get_ColWidth ;
//			grd_Grid.get_DataField ;
//			grd_Grid.get_RowHeight ;
//			grd_Grid.get_TextArray ;
			






			;




 


		}

		private void chk_YesOrNo_CheckedChanged(object sender, System.EventArgs e)
		{

			if (chk_YesOrNo.Checked==true) grd_Grid.set_TextMatrix(ActiveRow,ActiveCol,"√");	else
			grd_Grid.set_TextMatrix(ActiveRow,ActiveCol,"");

		}

		private void chk_YesOrNo_Leave(object sender, System.EventArgs e)
		{
				chk_YesOrNo.Visible =false;
		}









	}//ec
}//en
