DataGridView验证旧值而不是新值

时间:2010-05-12 14:56:06

标签: c# validation datagridview

我有一个绑定到DataTable的DataGridView,它有一个double的列,值必须介于0和1之间。这是我的代码

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_CellValidating触发dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].Value时,我的问题将包含编辑前的旧值,而不是新值。

例如,假设旧值为.1,然后输入3.上述代码在退出单元格时运行,dgvImpRDP_InfinityRDPLogin[e.ColumnIndex, e.RowIndex].Value .1 为该运行,代码验证并将 3 数据写入DataTable。

我再次点击它,尝试离开,这次它的行为应该是这样,它会提升单元格的错误图标并阻止我离开。我尝试输入正确的值(比如.7),但Value仍然是3,现在没有办法离开单元格,因为它由于错误而被锁定,我的验证代码永远不会推动新价值。

非常感谢任何建议。

编辑 - 新版本的代码基于Stuart的建议并模仿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;
        }
    }
}

2 个答案:

答案 0 :(得分:4)

您需要使用DataGridViewCellValidatingEventArgs实例的FormattedValue属性而不是单元格值,因为在验证成功之前不会更新单元格值:

  

用户输入的文字   用户界面(UI)成为   FormattedValue属性值。这是   您之前可以验证的值   它被解析为单元格值   适当的价值。 (MSDN

答案 1 :(得分: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;
           }
        }
     }
  }
相关问题