I have a column in a DGV that contains directories. When the user enters a directory, I want to validate the directory before I allow the user to continue. Currently, I'm trying to use the CellLeave event to handle this and everything works if I modify an exsiting row. If I enter an invalid directory in a new row and choose to not accept the change, I get an index out of range error.

I know that the problem is with the canceledit butI can't think of another way to do this. Any ideas?

vb Code:
  1. Private Sub dgvFiles_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFiles.CellLeave
  2.         Dim strDir As String
  3.         Dim strWarn As String
  4.         Dim strTitle As String
  5.  
  6.         If dgvFiles.Rows(e.RowIndex).IsNewRow Then Return
  7.  
  8.         strDir = CStr(dgvFiles.Rows(e.RowIndex).Cells("dir").EditedFormattedValue)
  9.         strWarn = strDir & " is not a valid directory." & vbCrLf & _
  10.                   "Do you want to retain this directory Value?"
  11.         strTitle = "Invalid Directory"
  12.  
  13.         If Not Directory.Exists(strDir) Then
  14.             If MessageBox.Show(strWarn, strTitle, MessageBoxButtons.YesNo, _
  15.                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) _
  16.                = Windows.Forms.DialogResult.No Then
  17.                 dgvFiles.CancelEdit()
  18.             End If
  19.         End If
  20.     End Sub