如题,小弟遇到这样一个难题:某个cell编辑状态结束后,判断数据出错,想使之重新恢复到编辑状态,而其他都不处于选中状态。用cell.Selected=true,效果不好啊。哪位大侠有更好的办法吗?

解决方案 »

  1.   

     this.dataGridView1.BeginEdit(false);
      

  2.   

    我是这样做的,但是效果怎么是焦点跑到下面的cell上了,呵呵!
      

  3.   

    你要先设置当然选中的单元格
    this.dataGridView1.CurrentCell = this.dataGridView1.Rows[0].Cells[0];
      

  4.   

    还是不行啊。当数据错误后,我弹出一个对话框,点击“确定”后,下一个单元格的内容变成了空。这是怎么回事啊?如果按Enter键,焦点还是下面,是不是要接受Enter的值啊!
      

  5.   

     this.dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
      

  6.   

    DataGridView dgv = (DataGridView)(sender);
                DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
                int value = (int)(cell.Value);
                
                if (value < 0 || value > 100)
                {
                    MessageBox.Show(this, "政治分数必须介于0到100之间!",
                        "数据出界!",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    
                    this.dataGridView1.CurrentCell = cell;
                    cell.Value = 0;
                    this.dataGridView1.BeginEdit(false);
                    
                }多谢兄弟了,呵呵!在线等!
      

  7.   

    CellValidating事件里面写验证
    如: private void GV_PriceInfo_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
              
          if (!String.IsNullOrEmpty(e.FormattedValue.ToString()))
          {
               String str = @"^(?:[0-9][0-9]*(?:\.[0-9]+)?|0(?:\.[0-9]+)?)$";
               if (!System.Text.RegularExpressions.Regex.IsMatch(e.FormattedValue.ToString(), str))
               {
                    MessageBox.Show("输入的字符串格式不正确");
                    GV_PriceInfo.CancelEdit();
                    e.Cancel = true;
                }
          }
    }
      

  8.   

    谢谢大哥,你的方法很好!不过又有了一个新问题,就是DataGridView启动后,默认的是第一行第一个单元格,结果验证了这个cell。而我想验证的仅仅是第四列,这怎么实现呢?多谢大哥赐教!我的QQ是:63292754.能否加兄弟为好友?
      

  9.   

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
            { 
                double d= 0.0; 
                dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty; 
                if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "") 
                { 
                    if(!double.TryParse(e.FormattedValue.ToString(),out d) || d<0.0) 
                    { 
                        dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!"; 
                        e.Cancel = true; 
                    } 
                } 
            } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
            { 
                dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty; 
            } 
    http://www.cnblogs.com/freeliver54/articles/903850.html