DataGridView单元格ComboBox控件添加事件

DataGridView中的ComboBox没有事件,更改了Combox中的值以后,必须用鼠标点一下别的地方(鼠标离开此单元格),才会走CellValueChanged事件...... 

 

解决思路是把这个没有事件的单元格,变成有事件的控件:

 1 //定义全局变量。
 2 int i_Enable;
 3 int i_Index;
 4 
 5 //给界面上的ComboBox控件注册Leave事件。
 6 private void cbo_Test_Leave(~~~)
 7 {
 8         i_Index=cbo_Test.SelectedIndex;
 9 }
10 
11 //给DataGridView注册CellClick事件。
12 private void dgv_Test_CellClick(~~~)
13 {
14         i_Index=cbo_Test.SelectedIndex;
15 }
16 
17 //给DataGridView注册EditingControlShowing事件。
18 private void dgv_Test_EditingControlShowing(~~~)
19 {
20         if(this.dgv_Test.CurrentCell.OwningColumn.Name="col_Test")
21         {
22                ((ComboBox)e.Control).SelectedIndexChanged+=new EventHandler(ComboBox_SelectedIndexChanged);
23         }
24 } 
25 
26 //定义ComboBox_SelectedIndexChanged事件。
27 private void ComboBox_SelectedIndexChanged(~~~)
28 {
29         string str=((ComboBox)sender).Text.Trim();
30         DataGridViewColumn tmpCol=dgv_Test.CurrentCell.OwningColumn;
31         if(tmpCol.Name="col_Test":)
32         {
33                 if(x=="v_Enable")
34                 {
35                         i_Enable=1;
36                 }
37                 else
38                 {
39                         DataGridViewRow tmpRow=dgv_Test.CurrentCell.OwningRow;
40                         for(int i=0;i<dgv_Test.Rows.Count-1;i++)
41                         {
42                                 if(dgv_Test.Rows[i].Cells["col_Test"].Value!=null&&
43                                    dgv_Test.Rows[i].Cells["col_Test"].Value.ToString().Trim()=="3")
44                                 {
45                                         if(i=tmpRow.Index)
46                                         {
47                                                 i_Enable=2;
48                                         }
49                                         else
50                                         {
51                                                 i_Enable=1;
52                                                 break;
53                                         }
54                                 }
55                                 else
56                                 {
57                                         i_Enable=2;
58                                 }
59                         }
60                 }
61                 if(i_Enable==1)
62                 {
63                         cbo_Test.Enable=true;
64                         cbo_Test.SelectedIndex=i_Index;
65                 }
66                 else
67                 {
68                         cbo_Test.Enable=false;
69                         cbo_Test.SelectedIndex=0;
70                 }
71         }
72 } 

 

由于使用的是EditingControlShowing事件,所以第一次点击会传一个空值,导致cbo_Test的值被清空。

为了解决这个问题,采用了一个全局变量来记录这个值,也就是相当于重新赋了一遍值,会闪一下。

有点遗憾......

 

不过,毕竟算是解决了问题,而且DataGridView中的二级联动也可以根据这个来实现。

所以,这个方法虽然不够完美,但应该还是有点儿参考价值的#^_^#~~~

转载于:https://www.cnblogs.com/liverpool/p/3895526.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值