Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i add up shakes entrees and barsfor each row now..ex. 12 + 12+ 12 the toal comes to 121212
VB
myTable.Columns.Add("Shake /Cereal", GetType(String))
        myTable.Columns.Add("Entrees", GetType(String))
        myTable.Columns.Add("Bars", GetType(String))
        myTable.Columns.Add("Total", GetType(String))
        myTable.Columns("Total").Expression = "[Shake /Cereal] + Entrees + Bars"


VB
Select Case e.ColumnIndex
           Case 5 To 7
               Dim myVal As Integer = 0
               Dim result As Boolean = Int32.TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, myVal)
               If Not result Then
                   MessageBox.Show("Please enter a numeric value.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                   DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
               Else
                   'proper value! use myVal
               End If
           Case Else
       End Select
Posted
Updated 22-Aug-13 0:50am
v2
Comments
TryAndSucceed 21-Aug-13 12:12pm    
Are you defining the column index somewhere? Coz you are looking for 5,6,7 where I see only 0,1,2,3.
PythonProgrammer 21-Aug-13 12:15pm    
i have more columns before the ones that are above
TryAndSucceed 21-Aug-13 14:16pm    
While debugging, is it entering your if/else statement?
Maciej Los 21-Aug-13 15:07pm    
Instead If... ElseIf.. End use Select... Case ... End.
PythonProgrammer 21-Aug-13 15:10pm    
tried that dim and for and datagrid cannot go between select and case

OK, before i answer the question, few notes:
1) bad practice - if some part of code is used more than once, you need to move it into separate procedure or function
VB
If (e.ColumnIndex = 5) Then   ' Checking numeric value for Column 5 only
        'see code below
ElseIf (e.ColumnIndex = 6) Then   ' Checking numeric value for Column 6 only
        'see code below
ElseIf (e.ColumnIndex = 7) Then   ' Checking numeric value for Column 7 only
    'code which was used 2 times before
    '<-- start
    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
'end -->
End if


2) bad practice - if you want to check value in a range of columns, use:
VB
If (e.ColumnIndex=5) Or (e.ColumnIndex=6) Or (e.ColumnIndex=7) Then
'...do something...
Else
'...do something...
End If

or
VB
 Select Case e.ColumnIndex
    Case 5, 6, 7
    '...do something...
    Case Else
    '...do something...
End Select

Select Case .... End Select[^]

3) bad practice - if you want to check if value is numeric, do not convert it into string and do not check it sign by sign, but please use Convert.To<TypeOfData>[^] or <TypeOfData>.Parse[^] or <TypeOfData>.TryParse[^] method
More about Parsing Numeric Strings[^]

Finally, the answer is: using TryParse method replace wrong input into numeric value (for example to 0 /zero/) ;)

[EDIT]

VB
Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

Select Case e.ColumnIndex
    Case 5 to 7
        Dim myVal AS Integer = 0
        Dim result As Boolean = Int32.TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value,myVal)
        If not result Then 
             'convertion failed
        Else
             'proper value! use myVal
        End If
    Case else
        'do nothing
End Select

End Sub


[/EDIT]
 
Share this answer
 
v2
Comments
PythonProgrammer 22-Aug-13 6:11am    
thank you very much that makes perfect sense. however when i input the parse into my code above nothing happens.. i dont know if i'm putting it in wrong or something
Maciej Los 22-Aug-13 6:18am    
Show me your code after changes ;)
PythonProgrammer 22-Aug-13 6:31am    
i tried using Conver.Int32(value)
Maciej Los 22-Aug-13 6:34am    
No, you don't. I have seen your code. You did learn nothing. Please, read my answer again and change the code to remove "bad practices". As i had mentioned, use Int32.TryParse() method!!!
PythonProgrammer 22-Aug-13 6:36am    
If (e.ColumnIndex = 5) Then ' Checking numeric value for Column 5 only
Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
Int32.TryParse()
Console.WriteLine("{0} --> {1}", value, number)
Else
Console.WriteLine("Unable to convert '{0}'", value)
End If
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
VB
Private Sub DataGridView1_DataError(sender As Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError

       If MessageBox.Show("Please enter a numeric value only.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Then
           e.Cancel = True
       End If

   End Sub
 
Share this answer
 

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