对象引用未设置为c#上的对象错误的实例 [英] Object reference not set to an instance of an object error on c#

查看:137
本文介绍了对象引用未设置为c#上的对象错误的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从repositoryItemGridLookUpEdit1_EditValueChanged事件中的gridview获取一个单元格值。但是我只得到空值然后错误显示如下对象引用未设置为该对象的实例。



如何特别获取单元格值在EditValueChanged事件中来自gridview的行?



我的代码是这个

Am trying to get a cell value from gridview in repositoryItemGridLookUpEdit1_EditValueChanged event. But am getting null value only then error show like this "Object reference not set to an instance of an object" in that line.

How to get cell value in particular row from gridview in EditValueChanged Event ?

my code is this

private void repositoryItemGridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
    {
        GridLookUpEdit LookupEdit = sender as GridLookUpEdit;
        DataRowView SelectedDataRow = (DataRowView)LookupEdit.GetSelectedDataRow();



        gridView1.SetFocusedRowCellValue("Description", SelectedDataRow["ProductDescription"]);
        gridView1.SetFocusedRowCellValue("UoM", SelectedDataRow["UnitofMeasure"]);
        gridView1.SetFocusedRowCellValue("Quantity", SelectedDataRow["DefaultQuantity"]);
        gridView1.SetFocusedRowCellValue("Price", SelectedDataRow["MRPPrice"]);
        gridView1.SetFocusedRowCellValue("TaxInPercentage", SelectedDataRow["Taxid1"]);
        gridView1.SetFocusedRowCellValue("ProductKind", SelectedDataRow["ProductKind"]);

        getdisc = LookupEdit.EditValue.ToString(); // get display value & pass to comparision to find Discound

        object productkin = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ProductDescrption"]);

        string productkind = productkin.ToString(); // Error Object reference not set to instance of object

        MessageBox.Show(productkind, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }





帮助我?



Help me ?

推荐答案

所以检查在调试器中查看该行:

So check in the debugger and look at the line:
object productkin = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ProductDescrption"]);

查看 FocusedRowHandle gridView1.Columns [ProductDescrption] 并查看他们想要返回的单元格。



我们不能 - 我们无法访问您的数据。

Look at the FocusedRowHandle, and gridView1.Columns["ProductDescrption"] and see what cell they are trying to return.

We can't - we don't have access to your data.






这是处理参考类型对象时的常见错误



请确保对象不是null,在访问其成员之前,方法..



试试这个..





而不是



Hi

this the common error while handling reference type object

Please make sure that the object is not null , before accessing its members , methods..

try this..


instead of

productkin.ToString(); 





使用



use

Convert.ToString(productkin);


这篇关于对象引用未设置为c#上的对象错误的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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