Click here to Skip to main content
15,888,124 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
How do i allow decimals in the below code?

VB
ElseIf (e.ColumnIndex = 9) Then   ' Checking numeric value for Column 9 only
            Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            For Each c As Char In value
                If Not Char.IsDigit(c) Then
                    MessageBox.Show("Please enter a numeric value.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
                    Exit Sub
                End If
            Next
Posted
Comments
[no name] 16-Sep-13 10:57am    
You change it to allow people to type in a "."
PythonProgrammer 16-Sep-13 11:00am    
yes but how do i do that
[no name] 16-Sep-13 11:01am    
What do you mean "how"? You write some code.
PythonProgrammer 16-Sep-13 11:02am    
I know that but what do i change char to
[no name] 16-Sep-13 11:06am    
I wouldn't have any idea what it is that you think that means. A . can be a char too.

1 solution

My VB is a bit rusty, but something like this should do it:

VB
ElseIf (e.ColumnIndex = 9) Then   ' Checking numeric value for Column 9 only
            Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            Dim dTmp As Decimal
            If Not Decimal.TryParse(value,dTmp) Then MessageBox.Show("Please enter a numeric value.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900