datagridview CellValidating事件问题 [英] datagridview CellValidating EVENT problem

查看:90
本文介绍了datagridview CellValidating事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家,

我正在使用CellValidating Event获取datagridview的一列的oldvalue.我的代码如下;

Hello Experts,

I am using CellValidating Event to get the oldvalue of a column of a datagridview.My code is as below;

private void dgvTnasctn_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
       {
           if (!bchk)
           {
               oldValue = Convert.ToInt32(dgvTnasctn[e.ColumnIndex, e.RowIndex].Value);
               if (e.FormattedValue != "")
               {
                   iold = oldValue;
                   inew = Convert.ToInt32(e.FormattedValue);
               }
               else
               {

                   newValue = 0;
               }
           }
       }



在上面的代码Iam中遇到问题,在gridview中的任意位置单击时,在



in above code Iam facing problem,while clicking anywhere in the gridview it shows the error "Input string was not in correct format" near

"inew = Convert.ToInt32(e.FormattedValue);"



我被困在这里,所以任何人都可以帮助我..



I am stuck here so anybody help me please..

推荐答案

inew = Convert.ToInt32(e.FormattedValue);

给出错误,因为
您已经处理了此条件e.FormattedValue,但没有此条件,
e.FormattedValue包含字母或特殊字符(+除外.)

无法将其转换为Int32

所以,
在转换为int32之前检查e.FormattedValue
像这样
inew = Convert.ToInt32(e.FormattedValue);

gives error because
you have handle this condition e.FormattedValue but not this,
e.FormattedValue that contains alphabet or special character (except +-.)

which can not be convert in to Int32

so,
check e.FormattedValue before converting in to int32
like this
if (e.FormattedValue.Trim() != "")// check it is empty
              {
                  if IsNumeric(e.FormattedValue.Trim())//check it contains char instead of numbers
                  {
                     iold = oldValue;
                     inew = Convert.ToInt32(e.FormattedValue.Trim());
                  }
              }


public static Boolean IsNumeric(string stringToTest)
{
    int result;
    return int.TryParse(stringToTest, out result);
}


祝您编码愉快!
:)


Happy Coding!
:)


这篇关于datagridview CellValidating事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆