1

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) 

End Sub

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

3
  • For the possible reasons of a NullReferenceException, see this post: stackoverflow.com/questions/4660142/…. That said, I suggest to run your application in Debug mode and check if any of your object references are Nothing. A candidate is dgv1.CurrentRow, another dgv1.CurrentCell. Commented Mar 8, 2014 at 6:45
  • Use a debugger to find the value which is Null. I suspect dgv1 is not initialized. Commented Mar 8, 2014 at 6:46
  • thanks I've done this: 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 handler in form load: AddHandler dgv2.CellValueChanged,AddressOf datagridview2_cellvaluechanged Commented Mar 9, 2014 at 9:59

1 Answer 1

0

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

End Sub

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.