Question

I'm hoping to find out a way to restrict a user from entering any non-numeric input into my datagridviewcolumn. Also I have already restricted the user from entering any negative numbers and from leaving the cell blank. If anyone can find a way to restrict the user from entering letters and non-numeric input I would greatly appreciate it!

If (e.ColumnIndex = 8) Then 'This specifies the column number
        Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            MessageBox.Show("Cannot Be Empty")
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0
        ElseIf cellData < 0 Then
            MessageBox.Show("Negatives Values Not Allowed")
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0

            Exit Sub

        End If
    End If
Was it helpful?

Solution 2

Give this a try. If you use the DataGridView.KeyPress event to monitor what was just typed, you can check what that character is with Char.IsDigit(e.KeyChar).

Private Sub DataGridView1_KeyPress(sender As Object, e As     System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
    If (Char.IsDigit(e.KeyChar)) Then
        'this is a number
    Else
        'not a number
    End If
End Sub

OTHER TIPS

You can always use Integer.TryParse().

You can read more about it here: http://msdn.microsoft.com/en-us/library/f02979c7.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top