winForm下的DataGrid控件存在很多不足之处,为了完善其控件,这里稍微重写了一下
可以按照这个思路去实现自己需要的功能,具体代码如下:
可以按照这个思路去实现自己需要的功能,具体代码如下:
1 using System;
2 using System.ComponentModel;
3 using System.Collections;
4 using System.Diagnostics;
5
6 namespace DataCenter
7 {
8 public delegate void MouseDownHandler(Object sender, System.Windows.Forms.MouseEventArgs e);
9 /// <summary>
10 /// ComGrid 的摘要说明。
11 /// </summary>
12 public class ComGrid : System.Windows.Forms.DataGrid
13 {
14 public event MouseDownHandler ClickWhere;
15
16 public void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
17 {
18 if (ClickWhere!=null)
19 ClickWhere(this,e);
20 System.Windows.Forms.DataGrid myGrid = (System.Windows.Forms.DataGrid)sender;
21
22 System.Windows.Forms.DataGrid.HitTestInfo hti = myGrid.HitTest(e.X, e.Y);
23
24 switch (hti.Type)
25 {
26 case System.Windows.Forms.DataGrid.HitTestType.None :
27 System.Windows.Forms.MessageBox.Show("the background");
28 break;
29 default:
30 System.Windows.Forms.MessageBox.Show("degault");
31 break;
32 }
33 }
34 public ComGrid(System.ComponentModel.IContainer container)
35 {
36 //
37 // Windows.Forms 类撰写设计器支持所必需的
38 //
39 InitializeComponent();
40 //
41 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
42 //
43 }
44
45 public ComGrid()
46 {
47 //
48 // Windows.Forms 类撰写设计器支持所必需的
49 //
50 InitializeComponent();
51 // MouseDown += new MouseDownHandler();
52
53 //
54 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
55 //
56 }
57 public void ShowMenu(string strString)
58 {
59 System.Windows.Forms.MessageBox.Show(strString);
60 }
61 #region 组件设计器生成的代码
62 /// <summary>
63 /// 设计器支持所需的方法 - 不要使用代码编辑器修改此方法的内容。
64 /// </summary>
65 private void InitializeComponent()
66 {
67
68 }
69 #endregion
70 }
71
72 public class Oversight //监视DataGrid_Click()
73 {
74 public Oversight()
75 {
76 }
77 public void ShowMenu(object sender,System.Windows.Forms.MouseEventArgs e)
78 {
79 try
80 {
81 ComGrid cgComGrid = ( YJDSoft.DataCenter.Grid.ComGrid)sender;
82
83 // System.Threading.Thread.Sleep(500);
84
85
86
87
88 System.Windows.Forms.DataGrid myGrid = (System.Windows.Forms.DataGrid) sender;
89
90
91 System.Windows.Forms.DataGrid.HitTestInfo hti = myGrid.HitTest(e.X, e.Y);
92 string message = "You clicked ";
93
94 switch (hti.Type)
95 {
96 case System.Windows.Forms.DataGrid.HitTestType.None :
97 message += "the background.";
98 break;
99 case System.Windows.Forms.DataGrid.HitTestType.Cell :
100 message += "cell at row " + hti.Row + ", col " + hti.Column;
101 break;
102 case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader :
103 message += "the column header for column " + hti.Column;
104 break;
105 case System.Windows.Forms.DataGrid.HitTestType.RowHeader :
106 message += "the row header for row " + hti.Row;
107 // if(e.Button == MouseButtons.Right)
108 // {
109 // this.conMenu.Show(dgDataGrid,new System.Drawing.Point(e.X,e.Y));
110 // }
111 break;
112 case System.Windows.Forms.DataGrid.HitTestType.ColumnResize :
113 message += "the column resizer for column " + hti.Column;
114 break;
115 case System.Windows.Forms.DataGrid.HitTestType.RowResize :
116 message += "the row resizer for row " + hti.Row;
117 break;
118 case System.Windows.Forms.DataGrid.HitTestType.Caption :
119 message += "the caption";
120 break;
121 case System.Windows.Forms.DataGrid.HitTestType.ParentRows :
122 message += "the parent row";
123 break;
124 }
125 cgComGrid.ShowMenu(message);
126
127 }
128 catch(Exception err)
129 {
130 DocCenter.formDocMain.Global_DocManagerLog.LogException(err);
131 }
132 }
133 }
134 }
135
2 using System.ComponentModel;
3 using System.Collections;
4 using System.Diagnostics;
5
6 namespace DataCenter
7 {
8 public delegate void MouseDownHandler(Object sender, System.Windows.Forms.MouseEventArgs e);
9 /// <summary>
10 /// ComGrid 的摘要说明。
11 /// </summary>
12 public class ComGrid : System.Windows.Forms.DataGrid
13 {
14 public event MouseDownHandler ClickWhere;
15
16 public void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
17 {
18 if (ClickWhere!=null)
19 ClickWhere(this,e);
20 System.Windows.Forms.DataGrid myGrid = (System.Windows.Forms.DataGrid)sender;
21
22 System.Windows.Forms.DataGrid.HitTestInfo hti = myGrid.HitTest(e.X, e.Y);
23
24 switch (hti.Type)
25 {
26 case System.Windows.Forms.DataGrid.HitTestType.None :
27 System.Windows.Forms.MessageBox.Show("the background");
28 break;
29 default:
30 System.Windows.Forms.MessageBox.Show("degault");
31 break;
32 }
33 }
34 public ComGrid(System.ComponentModel.IContainer container)
35 {
36 //
37 // Windows.Forms 类撰写设计器支持所必需的
38 //
39 InitializeComponent();
40 //
41 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
42 //
43 }
44
45 public ComGrid()
46 {
47 //
48 // Windows.Forms 类撰写设计器支持所必需的
49 //
50 InitializeComponent();
51 // MouseDown += new MouseDownHandler();
52
53 //
54 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
55 //
56 }
57 public void ShowMenu(string strString)
58 {
59 System.Windows.Forms.MessageBox.Show(strString);
60 }
61 #region 组件设计器生成的代码
62 /// <summary>
63 /// 设计器支持所需的方法 - 不要使用代码编辑器修改此方法的内容。
64 /// </summary>
65 private void InitializeComponent()
66 {
67
68 }
69 #endregion
70 }
71
72 public class Oversight //监视DataGrid_Click()
73 {
74 public Oversight()
75 {
76 }
77 public void ShowMenu(object sender,System.Windows.Forms.MouseEventArgs e)
78 {
79 try
80 {
81 ComGrid cgComGrid = ( YJDSoft.DataCenter.Grid.ComGrid)sender;
82
83 // System.Threading.Thread.Sleep(500);
84
85
86
87
88 System.Windows.Forms.DataGrid myGrid = (System.Windows.Forms.DataGrid) sender;
89
90
91 System.Windows.Forms.DataGrid.HitTestInfo hti = myGrid.HitTest(e.X, e.Y);
92 string message = "You clicked ";
93
94 switch (hti.Type)
95 {
96 case System.Windows.Forms.DataGrid.HitTestType.None :
97 message += "the background.";
98 break;
99 case System.Windows.Forms.DataGrid.HitTestType.Cell :
100 message += "cell at row " + hti.Row + ", col " + hti.Column;
101 break;
102 case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader :
103 message += "the column header for column " + hti.Column;
104 break;
105 case System.Windows.Forms.DataGrid.HitTestType.RowHeader :
106 message += "the row header for row " + hti.Row;
107 // if(e.Button == MouseButtons.Right)
108 // {
109 // this.conMenu.Show(dgDataGrid,new System.Drawing.Point(e.X,e.Y));
110 // }
111 break;
112 case System.Windows.Forms.DataGrid.HitTestType.ColumnResize :
113 message += "the column resizer for column " + hti.Column;
114 break;
115 case System.Windows.Forms.DataGrid.HitTestType.RowResize :
116 message += "the row resizer for row " + hti.Row;
117 break;
118 case System.Windows.Forms.DataGrid.HitTestType.Caption :
119 message += "the caption";
120 break;
121 case System.Windows.Forms.DataGrid.HitTestType.ParentRows :
122 message += "the parent row";
123 break;
124 }
125 cgComGrid.ShowMenu(message);
126
127 }
128 catch(Exception err)
129 {
130 DocCenter.formDocMain.Global_DocManagerLog.LogException(err);
131 }
132 }
133 }
134 }
135