对象引用未设置为显示对象错误的实例

时间:2014-03-08 06:40:48

标签: vb.net

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

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) 

End Sub

请告诉我如何解决此错误?

1 个答案:

答案 0 :(得分:0)

感谢大家现在我做到了这样: 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

End Sub

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