本帖最后由 chenguoxi 于 2010-10-29 00:13:38 编辑

解决方案 »

  1.   

    为什么不用CellValidating事件?
      

  2.   

    你的开关永远是关的,所以还是死循环。重新考虑你的开关设置地方吧。     private bool isEnter = false;
     //初始化为关闭状态 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
    {
     Test(sender, e);
     } private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) 
    {
     if( this.isEnter ) //如果开关打开,就直接Return了,不让CellEnter调用Test方法
     { 
    return; 

    else //只有在开关关闭时,CellEnter才能调用Test方法
     {
     Test(sender, e);
    this.isEnter = true; 
     }
     }
     private void Test(object sender, DataGridViewCellEventArgs e)
     { 
    //this.isEnter = true; //第一次进入,打开开关 if ( true ) //如果某种判断不成立,为了方便测试,我设定为true,也就是无论如何都不成立 { this.dataGridView1.Focus(); //执行到这里,由于isEnter开关是打开的,所以下面这条语句,执行了CellEnter方法, //CellEnter也没办法执行Test方法,也就无法造成死循环 this.dataGridView1.CurrentCell = this.dataGridView1[0, 0];
     } this.isEnter = false; //处理完毕,关闭开关 }//然后再在此列获得焦点时,才把开关关了。
      

  3.   

         private bool isEnter = false;
     //初始化为关闭状态 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
    {
     Test(sender, e);
     } private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) 
    {
     if( this.isEnter ) //如果开关打开,就直接Return了,不让CellEnter调用Test方法
     { 
    return; 

    else //只有在开关关闭时,CellEnter才能调用Test方法
     {
     Test(sender, e);
    this.isEnter = true; 
     }
     }
     private void Test(object sender, DataGridViewCellEventArgs e)
     { 
    //this.isEnter = true; //第一次进入,打开开关 if ( true ) //如果某种判断不成立,为了方便测试,我设定为true,也就是无论如何都不成立 { this.dataGridView1.Focus(); //执行到这里,由于isEnter开关是打开的,所以下面这条语句,执行了CellEnter方法, //CellEnter也没办法执行Test方法,也就无法造成死循环 this.dataGridView1.CurrentCell = this.dataGridView1[0, 0];
     } this.isEnter = false; //处理完毕,关闭开关 }//
      

  4.   

    这确实是一个Bug.
    我把isEnter从头到尾设置为true,而且取消一切把isEnter设置为false的语句,居然也报错。微软从来都是问题不断的,看看Windows的update就知道了,可惜VS2010,也是这样,真有点让人惋惜。。