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
If (e.ColumnIndex = 5) Then
ElseIf (e.ColumnIndex = 6) Then
ElseIf (e.ColumnIndex = 7) Then
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 if
2) bad practice - if you want to check value in a range of columns, use:
If (e.ColumnIndex=5) Or (e.ColumnIndex=6) Or (e.ColumnIndex=7) Then
Else
End If
or
Select Case e.ColumnIndex
Case 5, 6, 7
Case Else
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]
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
Else
End If
Case else
End Select
End Sub
[/EDIT]