Datagrid将字符串转换为Int
当我加起来为每一行摇动主菜和酒吧..ex。 12 + 12+ 12 toal to 121212
When i add up shakes entrees and barsfor each row now..ex. 12 + 12+ 12 the toal comes to 121212
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"
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
好的,在我回答问题之前,几点说明:
1)不良做法 - 如果某些部分代码被多次使用,则需要将其移至单独的程序或功能中
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 ' 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)不良做法 - 如果您想检查一系列列中的值,请使用:
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
'...do something...
Else
'...do something...
End If
或
or
Select Case e.ColumnIndex
Case 5, 6, 7
'...do something...
Case Else
'...do something...
End Select
Select Case .... End Select [ ^ ]
3)不良做法 - 如果你想检查价值是否为数字,请不要将其转换为字符串而不要按标志检查,但请使用 Convert.To< TypeOfData> [ ^ ]或< TypeOfData> .Parse [ ^ ]或< TypeOfData> .TryParse [ ^ ]方法
更多关于解析数字字符串 [ ^ ]
最后,答案是:使用 TryParse
方法将错误输入替换为数值(例如 0
/零/);)
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/) ;)
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]
[/EDIT]
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