Results 1 to 2 of 2

Thread: [RESOLVED] [2005] Validate Datagridview data

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Resolved [RESOLVED] [2005] Validate Datagridview data

    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

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Validate Datagridview data

    It never fails. Once I post a thread, I discover the answer.

    I moved my code to the CellValidating event and everything works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width