Results 1 to 4 of 4

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

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Resolved [RESOLVED] [2005] DataGridView Cell Check for number

    Greeting All,

    I have a DataGridView question:

    I have a DataGridView that has only one column that the user will use to input data and I want all data entered into this cell to be a number. I am using the following code to check the data entered into the cell during the CellLeave event. The event is firing and I am able to test the cell, but how can I stop the update process once I have tested the cell for a number. Currently, I have the DefaultDataError eventhandler set to do nothing, but it seams there would be a cleaner way.

    Thanks for your help.
    Code:
        Private Sub DataGridView3_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellLeave
            'Check for number values only in the Weight cell.
            Dim int_test As Short
    
            If Me.DataGridView3.CurrentCell.ColumnIndex = 2 Then
    
                Try
                    int_test = CShort(CStr(Me.DataGridView3.CurrentCell.Value)) 'Test for number values
                Catch ex As Exception
                    MessageBox.Show("Invalid Weight")
                    Exit Sub
                End Try
            End If
        End Sub
    
        Private Sub DataGridView3_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView3.DataError
    
            'do nothing
    
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DataGridView Cell Check for number

    You shouldn't be using CellLeave. You should be using CellValidating.

    Also, to ensure that a string is a valid representation of a number you should not be converting and catching exceptions on failure. You should be calling the TryParse method of the appropriate numeric type. If the number needs to be a valid Short then use Short.TryParse.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Red face Re: [2005] DataGridView Cell Check for number

    Thanks for the quick reply. I am not familiar with the TryParse method, so I have some LIB reading to do. Thanks.

  4. #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