0

I have a DataGridView control that has a TextBox Column. On the DataGridView.CellValidating event, I use the following code to verify that the entry is an acceptable date:

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

This code works fine the first time the cell is edited.

The trouble that I am having is that IF I enter a valid date, and then later come back to the cell and enter an invalid date, on the CellValidating event, it skips over this If...End If block because it shows that the current cell value is the old valid date, not the invalid entry that is currently in the cell.

How do I commit or get the invalid value that currently resides in the DataGridView cell?

Thanks

J2Tuner
  • 411
  • 1
  • 5
  • 17

1 Answers1

1

Use the CellValidated event instead.

CellValidating event is called before replacing the new cell value

Mathter
  • 747
  • 7
  • 14
  • This does not seem to work. If I type in a correct datetime format, such as 8:00, the cell will be validated. The next time I enter an invalid datetime in that same cell, such as "8", the CellValidating event will run and not error out, but the CellValidated event will not run. Is there some event that happens in between these two? Is there a way to view an error that may be preventing the CellValidated event from running? – J2Tuner Apr 24 '13 at 13:26