2010-05-12 31 views
5

我有一个绑定到一个DataTable一个DataGridView,它有一个列,它是一个双和值需要在0和1之间。这里是我的代码DataGridView的验证旧值insted的新价值

private void dgvImpRDP_InfinityRDPLogin_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    if (e.ColumnIndex == dtxtPercentageOfUsersAllowed.Index) 
    { 
     double percentage; 
     if(dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].Value.GetType() == typeof(double)) 
      percentage = (double)dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].Value; 
     else if (!double.TryParse(dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].Value.ToString(), out percentage)) 
     { 
      e.Cancel = true; 
      dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].ErrorText = "The value must be between 0 and 1"; 
      return; 
     } 
     if (percentage < 0 || percentage > 1) 
     { 
      e.Cancel = true; 
      dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].ErrorText = "The value must be between 0 and 1"; 
     } 
    } 
} 

但是我的问题dgvImpRDP_InfinityRDPLogin_CellValidatingdgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].Value将包含编辑前的旧值,而不是新值。

例如可以说旧值是1.1和我进入3,上面的代码,当你退出细胞和dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].Value0.1该运行运行,代码验证和写入数据到DataTable。

我再次点击它,尝试离开,这次它表现得像它应该的那样,它引发了单元格的错误图标,并阻止我离开。我尝试输入正确的值(比如.7),但Value仍然是3,现在没有出路的单元格,因为它由于错误而被锁定,我的验证码永远不会推送新值。

任何建议将不胜感激。

编辑 - 新版本的代码基于斯图尔特的建议和模仿MSDN文章使用的风格。仍然表现相同。

private void dgvImpRDP_InfinityRDPLogin_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    if (e.ColumnIndex == dtxtPercentageOfUsersAllowed.Index) 
    { 
     dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].ErrorText = String.Empty; 
     double percentage; 
     if (!double.TryParse(dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].FormattedValue.ToString(), out percentage) || percentage < 0 || percentage > 1) 
     { 
      e.Cancel = true; 
      dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].ErrorText = "The value must be between 0 and 1"; 
      return; 
     } 
    } 
} 

回答

4

您需要使用DataGridViewCellValidatingEventArgs实例,而不是单元格的值的FormattedValue的属性,如不更新单元格的值,直到验证成功:

通过用户输入的文本用户界面(UI)变为 FormattedValue属性值。这是 可以在 之前验证的值,它被解析为单元格值 属性值。 (MSDN

+0

不,'dgvImpRDP_InfinityRDPLogin [e.ColumnIndex,e.RowIndex] .FormattedValue' =” 0.1" 之后我已进入5 – 2010-05-12 15:06:20

+1

不。你需要看看e.FormattedValue! – stuartd 2010-05-12 16:22:38

+0

啊!谢谢我甚至没有注意到e中的格式化值。 – 2010-05-12 17:55:49

1

如何做这样的事情?这假设你在你的datagridview中使用了文本框,所以如果你正在使用其他控件,只需将其更改为。 (虽然我不确定Stuart Dunkeld为什么没有工作,但FormattedValue应该具有新的价值)。

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
    { 
    if (e.ColumnIndex == 0) // dtxtPercentageOfUsersAllowed.Index 
    { 
     object should_be_new_value = e.FormattedValue; 
     double percentage; 
     if (dgvImpRDP_InfinityRDPLogin.EditingControl != null) 
     { 
      string text = dgvImpRDP_InfinityRDPLogin.EditingControl.Text; 
      if (!double.TryParse(text, out percentage)) 
      { 
       e.Cancel = true; 
       dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].ErrorText = "The value must be between 0 and 1"; 
       return; 
      } 
      if (percentage < 0 || percentage > 1) 
      { 
       e.Cancel = true; 
       dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].ErrorText = "The value must be between 0 and 1"; 
      } 
      else 
      { 
       dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].ErrorText = null; 
      } 
     } 
    } 
    } 
+0

工作。谢谢。 – 2010-05-12 15:24:32

+0

没问题。我只是稍微改变了一下使用文本而不检查类型。应该只给出相同的结果一点清洁剂,但让我知道如果它不起作用,我会恢复发回。 – SwDevMan81 2010-05-12 15:30:17

+0

它失败,因为如果您正在验证EditingControll可以为null,并且当前没有正在编辑的框。 – 2010-05-12 15:56:02