Results 1 to 4 of 4

Thread: [RESOLVED] [2005] DataGridView Cell Check for number

Threaded View

  1. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Re: [2005] DataGridView Cell Check for number

    Well, I should not have marked this as resolved so quickly.

    When using CellValidating, the TryParse method can not be tested because the cell contents do not exist yet... unless there is a way to read the grid cell contents before the cell is validated. This being the case, there is no point in handling the CellValidating event at all, because the default Grid DataError event will fire if the CellValidating process fails to meet the requirements of the DataTable. Here is the code I am trying now. Am I still missing the bigger picture or is is simply trapping the Grid DataError the correct procedure?
    Thanks

    Code:
        Private Sub DataGridView3_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView3.CellValidating
            'Check for number values only in the Weight cell.
    
            If Me.DataGridView3.CurrentCell.ColumnIndex = 2 Then
                Dim singleVal As Single
                Dim result As Boolean
                Dim str_testString As String = ""
    
                If Not Me.DataGridView3.CurrentCell.Value Is Nothing And Not IsDBNull(Me.DataGridView3.CurrentCell.Value) Then
                    str_testString = CStr(Me.DataGridView3.CurrentCell.Value)
    
                    result = [Single].TryParse(str_testString, singleVal)
                    Console.WriteLine(result & ", " & singleVal.ToString())
    
                    If result = False Then
                        MessageBox.Show("Invalid Weight")
                        e.Cancel = True
                    End If
                End If
            End If
        End Sub
    
        Private Sub DataGridView3_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView3.DataError
    
            MsgBox("Error Trapped")
    
        End Sub
    Last edited by MechEng; Jan 5th, 2008 at 11:45 AM.

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