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:
Private Sub dgvFiles_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFiles.CellLeave Dim strDir As String Dim strWarn As String Dim strTitle As String If dgvFiles.Rows(e.RowIndex).IsNewRow Then Return strDir = CStr(dgvFiles.Rows(e.RowIndex).Cells("dir").EditedFormattedValue) strWarn = strDir & " is not a valid directory." & vbCrLf & _ "Do you want to retain this directory Value?" strTitle = "Invalid Directory" If Not Directory.Exists(strDir) Then If MessageBox.Show(strWarn, strTitle, MessageBoxButtons.YesNo, _ MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) _ = Windows.Forms.DialogResult.No Then dgvFiles.CancelEdit() End If End If End Sub




Reply With Quote