【问题标题】:object reference not set to an instance of an object error showing对象引用未设置为对象错误显示的实例
【发布时间】:2014-04-11 13:03:05
【问题描述】:

每当我运行以下代码时,它都会将错误显示为“对象引用未设置为对象的实例”: (此代码根据同一行中的其他 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) 

结束子

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

【问题讨论】:

  • 对于 NullReferenceException 的可能原因,请参阅此帖子:stackoverflow.com/questions/4660142/…。也就是说,我建议在调试模式下运行您的应用程序并检查您的任何对象引用是否为Nothing。一个候选人是dgv1.CurrentRow,另一个是dgv1.CurrentCell
  • 使用调试器查找 Null 的值。我怀疑 dgv1 没有初始化。
  • 谢谢我这样做了: 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 ' 我们可以取 STRING 或 OBJECT var 是强制性的 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表单加载:AddHandler dgv2.CellValueChanged,AddressOf datagridview2_cellvaluechanged

标签: vb.net


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-07-09
    • 2012-08-31
    • 2011-11-21
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    相关资源
    最近更新 更多