将datagridview数据传递给另一个datagridview'System.NullReferenceException'

时间:2017-11-05 06:04:39

标签: vb.net

我需要这个问题的帮助,我不知道如何解决这个问题。

这是代码;

Private Sub GetLabandOtherFees()
        Dim dic As New Dictionary(Of String, Integer)()
        Dim cellValue As String = Nothing
        For i As Integer = 0 To dgvSubjectsEnrolled.Rows.Count - 1
            If Not dgvSubjectsEnrolled.Rows(i).IsNewRow Then
                cellValue = dgvSubjectsEnrolled(0, i).Value.ToString()
                If Not dic.ContainsKey(cellValue) Then
                    dic.Add(cellValue, 1)
                Else
                    dic(cellValue) += 1
                End If
            End If
        Next

        Dim sb As New StringBuilder()
        For Each keyvalue As KeyValuePair(Of String, Integer) In dic
            sb.AppendLine(String.Format("{0}", keyvalue.Key, keyvalue.Value))
        Next

        For Each row As DataGridViewRow In dgvsub.Rows
            row.Cells(0).Value = sb.ToString()
            row.Cells(1).Value = dic.Count.ToString()
        Next
    End Sub

这就是错误;

"Object Reference Not set to an instance of an object." 
Check to determine if object is null before calling the method.

这个错误令我头疼。请帮忙。

1 个答案:

答案 0 :(得分:0)

阅读错误中的内容。在这里,尝试这个,只需在其中添加一些条件。

               If dgvSubjectsEnrolled(0, i).Value > 0 Then
                    cellValue = dgvSubjectsEnrolled(0, i).Value.ToString()
                    If Not dic.ContainsKey(cellValue) Then
                        dic.Add(cellValue, 1)
                    Else
                        dic(cellValue) += 1
                    End If
                Else
                End If
相关问题