zoukankan      html  css  js  c++  java
  • 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中的二级联动也可以根据这个来实现。

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

  • 相关阅读:
    Max History CodeForces
    Buy a Ticket CodeForces
    AC日记——字符串的展开 openjudge 1.7 35
    AC日记——回文子串 openjudge 1.7 34
    AC日记——判断字符串是否为回文 openjudge 1.7 33
    AC日记——行程长度编码 openjudge 1.7 32
    AC日记——字符串P型编码 openjudge 1.7 31
    AC日记——字符环 openjudge 1.7 30
    AC日记——ISBN号码 openjudge 1.7 29
    AC日记——单词倒排 1.7 28
  • 原文地址:https://www.cnblogs.com/liverpool/p/3895526.html
Copyright © 2011-2022 走看看