在输入有效条目后在DataGridView Cell中输入无效条目时,如何获取当前单元格值?

时间:2013-04-23 13:58:18

标签: vb.net datagridview datagridviewtextboxcell

我有一个具有TextBox列的DataGridView控件。在DataGridView.CellValidating事件中,我使用以下代码来验证该条目是否为可接受的日期:

dgvStartingGrid.EndEdit()
Dim dteStartTime As DateTime
If Not Date.TryParse(dgvStartingGrid.CurrentRow.Cells(4).Value, dteStartTime) Then
    MessageBox.Show("Please enter a starting time in one of the following formats:" & vbCrLf & _
    vbCrLf & "- MM/DD/YY HH:MM:SS (24 Hour Format)" & vbCrLf & "- HH:MM:SS (24 Hour Format)" & _
    vbCrLf & "- HH:MM:SS (AM/PM) (12 Hour Format)", "Start Time Entry Error")
    e.Cancel = True
End If

此代码在第一次编辑单元格时工作正常。

我遇到的麻烦是,如果我输入一个有效的日期,然后又回到单元格并输入一个无效的日期,在CellValidating事件中,它会跳过这个If ... End If块,因为它显示当前单元格值是旧的有效日期,而不是当前在单元格中的无效条目。

如何提交或获取当前驻留在DataGridView单元格中的无效值?

由于

1 个答案:

答案 0 :(得分:1)

改为使用CellValidated事件。

在替换新单元格值

之前调用CellValidating事件