DataGridView 中的最后一行验证
Last row verification in DataGridView
我有这段代码可以验证 DataGridView 中的行。它检查该行中单元格的内容是否在 0 到 10 之间,如果是 11+,它将突出显示为红色。
我遇到的问题是,即使所有数字都在 0-10 之内,该行的最后一行也会突出显示为红色。
这是某种文件结束的情况吗?
在代码中,如果单元格为空,则什么也不会发生。所以我认为最后一行也应该什么都不做(只要它是 0-10)
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.Cells("F3").Value Is DBNull.Value Then
Dim cellNumber As Integer
If Integer.TryParse(row.Cells("F3").Value, cellNumber) AndAlso cellNumber >= 0 AndAlso cellNumber <= 10 Then
'All pass verification, Do nothing
Else
'Point out the wrong value
row.Cells("F3").Style.BackColor = Color.Red
End If
Else
' MessageBox.Show("Not a number!")
End If
Next
在您的 CellValidating 事件中,您应该检查 ColumnIndex 是否是所需的列并且当前行是否不是新行。之后你应该检查该值是否在你的范围内。
看看这个例子(我不知道索引 F3 是什么,所以我假设为 0):
Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If e.ColumnIndex = 0 AndAlso Not DataGridView1.Rows.Item(e.RowIndex).IsNewRow Then
Dim value As String = e.FormattedValue.ToString()
Dim numericValue As Integer
With DataGridView1.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Style
If Integer.TryParse(e.FormattedValue.ToString, numericValue) AndAlso numericValue >= 0 AndAlso numericValue <= 10 Then
.BackColor = Color.White
.ForeColor = Color.Black
Else
.BackColor = Color.Red
.ForeColor = Color.White
End If
End With
End If
End Sub
我找到了我遗漏的那一行..
我必须添加:
AndAlso Not row.Cells("F11").value Is Nothing
有:
If Not row.Cells("F3").Value Is DBNull.Value AndAlso Not row.Cells("F11").value is Nothing Then
我有这段代码可以验证 DataGridView 中的行。它检查该行中单元格的内容是否在 0 到 10 之间,如果是 11+,它将突出显示为红色。
我遇到的问题是,即使所有数字都在 0-10 之内,该行的最后一行也会突出显示为红色。 这是某种文件结束的情况吗?
在代码中,如果单元格为空,则什么也不会发生。所以我认为最后一行也应该什么都不做(只要它是 0-10)
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.Cells("F3").Value Is DBNull.Value Then
Dim cellNumber As Integer
If Integer.TryParse(row.Cells("F3").Value, cellNumber) AndAlso cellNumber >= 0 AndAlso cellNumber <= 10 Then
'All pass verification, Do nothing
Else
'Point out the wrong value
row.Cells("F3").Style.BackColor = Color.Red
End If
Else
' MessageBox.Show("Not a number!")
End If
Next
在您的 CellValidating 事件中,您应该检查 ColumnIndex 是否是所需的列并且当前行是否不是新行。之后你应该检查该值是否在你的范围内。
看看这个例子(我不知道索引 F3 是什么,所以我假设为 0):
Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If e.ColumnIndex = 0 AndAlso Not DataGridView1.Rows.Item(e.RowIndex).IsNewRow Then
Dim value As String = e.FormattedValue.ToString()
Dim numericValue As Integer
With DataGridView1.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Style
If Integer.TryParse(e.FormattedValue.ToString, numericValue) AndAlso numericValue >= 0 AndAlso numericValue <= 10 Then
.BackColor = Color.White
.ForeColor = Color.Black
Else
.BackColor = Color.Red
.ForeColor = Color.White
End If
End With
End If
End Sub
我找到了我遗漏的那一行.. 我必须添加:
AndAlso Not row.Cells("F11").value Is Nothing
有:
If Not row.Cells("F3").Value Is DBNull.Value AndAlso Not row.Cells("F11").value is Nothing Then