【问题标题】:DataGridView & 'System.NullReferenceException' errorDataGridView & 'System.NullReferenceException' 错误
【发布时间】:2012-03-21 08:40:09
【问题描述】:

我正在尝试使用我的 DataGridView 值加载我的文本框,如下所示,但我得到了 “在 Microsoft.VisualBasic.dll 中发生了‘System.NullReferenceException’类型的第一次机会异常” 错误,尽管如此,该程序仍然可以使用。有谁知道出了什么问题?

非常感谢。

代码:

For iloop = 0 To DataGridView2.Rows.Count
        For Each cCtrl As Control In Panel2.Controls
            If TypeOf cCtrl Is TextBox Then
                Dim txtBox As TextBox
                txtBox = cCtrl
                If (txtBox.Name.Substring(9, 6)) = ((DataGridView2.Rows.Item(iloop).Cells(0).Value).substring(0, 6)) Then
                    txtBox.Text = DataGridView2.Rows.Item(iloop).Cells(3).Value
                End If
            End If
        Next
    Next

【问题讨论】:

  • 哪一行出现错误?
  • 嗨,这一行是:“If (txtBox.Name.Substring(9, 6)) = ((DataGridView2.Rows.Item(iloop).Cells(0).Value).substring( 0, 6)) Then" ((DataGridView2.Rows.Item(iloop).Cells(0).Value).substring(0, 6)) 实际上是导致它的原因。

标签: .net vb.net


【解决方案1】:

在获取值的子字符串之前,为If DataGridView2.Rows.Item(iloop).Cells(0).Value IsNot Nothing Then 添加一个 if 条件。

【讨论】:

  • 谢谢,但检查 null 并没有帮助,我只是发现如果我要分配 '((DataGridView2.Rows.Item(iloop).Cells(0).Value).substring(0 , 6))' 到一个字符串变量,我会得到那个异常错误。
猜你喜欢
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多