Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
how do i get this code to accept numbers 0-7 only
If (e.ColumnIndex = 4) Then   ' Checking numeric value for Column 4 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 number 0-7 only.", "Input Error", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
            Exit Sub
        End If
    Next
Posted
Updated 15-Aug-13 6:13am
v2
Comments
JasonMacD 15-Aug-13 12:27pm    
It may be easier to use a drop down list, and let the user select hard coded values between 0-7. You will then have the SelectedIndex and SelectedValue with which you can work with. Just a thought. If it were a much larger range then a textbox would be optimal
PythonProgrammer 15-Aug-13 12:28pm    
that is what i was going to do but the company does not want me to do that

1 solution

Try:
VB
For Each c As Char In value
    Dim i As Integer = -1
    If Not (Int32.TryParse(c, i) AndAlso i >= 0 AndAlso i <= 7) Then
        ' your other code here


[UPDATE]

If you don't want to split the value into seperate chars, try this:
VB
Dim i As Integer = -1
If Not (Int32.TryParse(value, i) AndAlso i >= 0 AndAlso i <= 7) Then
    ' your other code here

If you use this code, DON'T use a For Each loop!
 
Share this answer
 
v2
Comments
PythonProgrammer 15-Aug-13 12:31pm    
i just used that you just gave me..however if i do a number like 12 it does not show the error
Thomas Daniels 15-Aug-13 12:34pm    
The reason is: '12' is split in two chars: '1' and '2'. Because both are between 0-7, you don't get an error. I'll update my answer.
Thomas Daniels 15-Aug-13 12:36pm    
I updated my answer. Use the second code.
PythonProgrammer 15-Aug-13 12:35pm    
thank you very much
Thomas Daniels 15-Aug-13 12:36pm    
You're welcome!

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