OnTextChanged得到错误 [英] OnTextChanged get error

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

问题描述

  private   void  txt_money_TextChanged(对象发​​件人,EventArgs e)
{
double price = Double .Parse(dataGridView1.CurrentRow.Cells [ Price]。Value.ToString( ));
double getoil;
double money = Double .Parse(txt_money.Text);
getoil = price / money;
lb_getoil.Text = + getoil;
lb_allpay.Text = txt_money.Text;
}



我的代码我收到错误



 对象引用 set  object的实例



at

  double  price =  Double  .Parse(dataGridView1.CurrentRow.Cells [ 价格]。Value.ToString()); 





如何解决?

解决方案

请记住每次Text属性获取新值时都会触发Textbox.TextChanged事件 - 因此您将在程序开始时获得一个,而系统的其余部分是设置好或不。



请检查您的代码:

 dataGridView1.CurrentRow。单元格[ 价格]。Value.ToString()

是否有CurrentRow?在程序开始时,可​​能没有,因为DataGridView还没有填充数据。首先检查:

  private   void  txt_money_TextChanged( object  sender,EventArgs e)
{
if (dataGridView1.CurrentRow != null
{
...
}
}


private void txt_money_TextChanged(object sender, EventArgs e)
        {
            double price = Double.Parse(dataGridView1.CurrentRow.Cells["Price"].Value.ToString());
            double getoil;
            double money = Double.Parse(txt_money.Text);
            getoil = price / money;
            lb_getoil.Text = "" + getoil;
            lb_allpay.Text = txt_money.Text;
        }


This my code i got error

Object reference not set to an instance of an object.


at

double price = Double.Parse(dataGridView1.CurrentRow.Cells["Price"].Value.ToString());



How to fix ?

解决方案

Bear in mind that the Textbox.TextChanged event gets fired each time teh Text property gets a new value - so you will get one at the start of your program, wether the rest of your system is set up for it yet or not.

So check in your code:

dataGridView1.CurrentRow.Cells["Price"].Value.ToString()

Is there a CurrentRow? At the start of your program, there probably isn't, because the DataGridView has not been filled with data yet. So check first:

private void txt_money_TextChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                ...
            }
        }


这篇关于OnTextChanged得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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