未将对象引用设置为对象错误的实例,显示 [英] object reference not set to an instance of an object error showing

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

问题描述

每当我运行以下代码时,它都会将错误显示为未将对象引用设置为对象的实例":(此代码根据同一行中的其他 DatagridViewComboBox 并共享相同的数据库表更改 DatagridViewComboBox 的值.)

Whenever I run the following code it shows the error as "object reference not set to an instance of an object" : (This codes change the value of DatagridViewComboBox as per other DatagridViewComboBox in same row and sharing same databse table.)

Private Sub dgv1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellValueChanged

    Try

        Dim currentrowindex As Integer = dgv1.CurrentRow.Index
        Dim obj As Object = dgv1.CurrentCell.Value           
        Me.dgv1(1, currentrowindex).Value = obj
        Me.dgv1(2, currentrowindex).Value = obj
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub



Private Sub dgv1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.CurrentCellDirtyStateChanged
   dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit) 

结束子

请告诉我如何解决这个错误???

Please tell me how can I fix this error ???

推荐答案

感谢大家现在我已​​经这样做了:Private Sub datagridview2_cellvaluechanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

thanks to everyone now I have done this as : Private Sub datagridview2_cellvaluechanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

    Try
        Dim currentrowindex As Integer = dgv2.CurrentRow.Index
        Dim obj As Object = dgv2.CurrentCell.Value   ' we can take STRING or OBJECT var is mandatory

        Me.dgv2(4, currentrowindex).Value = obj
        Me.dgv2(5, currentrowindex).Value = obj
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

结束子

并在表单加载中添加一个处理程序:AddHandler dgv2.CellValueChanged, AddressOf datagridview2_cellvaluechanged

and add a handler in form load : AddHandler dgv2.CellValueChanged, AddressOf datagridview2_cellvaluechanged

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

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