stackexchange12
stackexchange12

Reputation: 652

How to restrict special characters while allowing doubles in datagridview using vb.net

I have a datagridview windows forms application using vb.net that restricts certain user input such as characters, special characters, punctuation and negative numbers. However when I restrict input such as this it also restricts the use of any numbers with decimal places. The "." symbol is always rejected. Is there anyway to reject "." unless its in combination with a numeric value? The code below is a short snippit of my validation check for non-numerical input, however it doesn't recognize numerical values with decimal places (i.e. ".") as being numerical. Is there anything I can do to avoid this issue?

Dim value As String = DataGridView1.Rows(rowindex).Cells(columnindex).Value.ToString

                For Each c As Char In value
                    If Not Char.IsDigit(c) Then
                            MessageBox.Show("Not a Valid Entry")
                        Else
                        'Default value provided after validation
                        DataGridView1.Rows(rowindex).Cells(columnindex).Value = 0.5

                    End If
                Next

Upvotes: 0

Views: 908

Answers (1)

Luc Morin
Luc Morin

Reputation: 5380

You might want to use VB.NET's IsNumeric() method, which will handle your scenario with just one single check, instead of checking char by char.

Cheers

Upvotes: 1

Related Questions