有人能说出代码有什么问题吗?

时间:2014-04-16 08:12:00

标签: vb.net

此按钮用于检查最后一行中是否有空单元格。 如果有填充单元格,则按钮(确认器)将被启用..当我调试时应用程序崩溃

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        DataGridView1.AllowUserToAddRows = "true"
        DataGridView1.ClearSelection()
        DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
        Dim c As Boolean
        Dim D As String
        Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count - 1

        c = Confirmer.Enabled
        Do
            For i As Integer = 0 To nbrcell - 1
                D = DataGridView1.SelectedRows(0).Cells(i).Value
                If D <> "" Then

                    c = True
                End If
            Next
        Loop Until c = True
    End Sub

3 个答案:

答案 0 :(得分:0)

您正在尝试从CurrentRow属性中读取单元格数 但是如果在运行此代码时没有设置CurrentRow,则代码会因Null Reference Exception而崩溃。 (A.K.A.对象引用未设置为对象的实例)

相反,你应该使用NewRowIndex的行。

Dim nbrcell As Integer = DataGridView1.Rows(DataGridView1.NewRowIndex).Cells.Count

并且不要从细胞计数中减去1。你这样做了两次。一个是你得到计数而另一个当你循环计数时(所以不检查最后一个单元格)

现在循环:如果单元格全部填满,你永远不会退出该循环

Dim D as Object
Dim allFilled = True
For i As Integer = 0 To nbrcell - 1
    D = DataGridView1.SelectedRows(0).Cells(i).Value
    If D Is Nothing OrElse 
        Convert.IsDBNull(D) OrElse 
        String.IsNullOrWitheSpace(D.ToString()) Then
        allFilled = False
              Exit For ' no need to continue the loop
    End If
Next
Confirmer.Enabled = allFilled

我已经移除了外部,而循环仅使用for执行,并且只要找到具有Nothing,DBNull或空字符串值的单元格就会停止。此时,布尔变量设置为false并应用于按钮的Enabled属性。如果没有发现单元格无效,则boolen变量保持设置为true,并且该按钮将被启用。

可能有可能在单元格的评估代码中更改某些内容,但这取决于您如何填充网格(手动或通过数据库数据)

答案 1 :(得分:0)

我已经改变了你的代码:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    DataGridView1.AllowUserToAddRows = "true"
    DataGridView1.ClearSelection()
    DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
    Dim c As Boolean
    Dim D As Object
    Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count - 1

    c = Confirmer.Enabled
    Do
        For i As Integer = 0 To nbrcell - 1
            D = DataGridView1.SelectedRows(0).Cells(i).Value
            If Not IsDBNull(D) Then

                c = True
            End If
        Next
    Loop Until c = True
End Sub

希望有所帮助

答案 2 :(得分:0)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Confirmer.Visible = True
Confirmer.Enabled = False

DataGridView1.AllowUserToAddRows = "true"
DataGridView1.ClearSelection()

DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True


Dim c As Boolean = False
Dim D As Object
Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count
Do
    For i As Integer = 0 To nbrcell - 1
        D = DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(i).Value
        If Not IsDBNull(D) Then
            Confirmer.Enabled = True
            c = True
        End If
    Next
Loop Until c = True

 End Sub

现在的问题是,即使单元格为空,按钮(确认器)也始终处于启用状态