如何处理DataGridView的所有行而不是仅第一行

时间:2018-07-15 10:41:46

标签: vb.net

以下代码仅影响DataGridView的第一行,但我想影响所有行。有人可以帮我吗?

Private arLetterChars() As Char = "ÇÈÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÝÞßáãäåæí"
Private arEncryptedChars() As Char = "أبتثجحخدذرزسشصضطظعغفقكتمنهوي"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    With GV_Products.Rows(0).Cells(7).ToString

        For Each myTextBoxChar As Char In GV_Products.Rows(0).Cells(7).Value.ToString 
            For i As Integer = 0 To arLetterChars.Length - 1 
                If myTextBoxChar = arLetterChars(i) Then GV_Products.Rows(0).Cells(7).Value = GV_Products.Rows(0).Cells(7).Value.Replace(myTextBoxChar, arEncryptedChars(i))
            Next
        Next

    End With

End Sub

我更改了如下代码

For Each row As DataGridViewRow In GV_Products.Rows
    For Each myTextBoxChar As Char In row.Cells(5).Value.ToString
        For i As Integer = 0 To arLetterChars.Length - 1
           If myTextBoxChar = arLetterChars(i) Then row.Cells(5).Value = row.Cells(5).Value.Replace(myTextBoxChar, arEncryptedChars(i))
        Next
    Next
 Next

但仍然发生此错误System.NullReferenceException:'对象引用未设置为对象的实例。

1 个答案:

答案 0 :(得分:3)

您显示的错误通常是由于将“允许用户添加行”设置为True。这是datagridview底部的额外行,允许用户继续添加行。

如果不需要,可以将其设置为false,但是我更喜欢在代码中进行检查,以防日后需要打开它而忘记进行检查。

您只需要将代码更改为



implementation 'com.mcxiaoke.volley:library-aar:1.0.0'