首页 > 解决方案 > 验证数字数据输入时控制数据网格视图的逗号输入

问题描述

我有一个数据网格视图,我必须将其中一列的输入限制为数字输入(整数和小数位)。用户将值直接输入到数据网格视图中。总共有 2 列,其中一列是只读的。我现在有以下代码:

Private Sub dgv1_CellEndEdit(By Val sender As System.Object, ByVal e As System.Windows.Form.DataGridViewCellEventArgs) Handles dgv1.CellEndEdit

    If (e.ColumnIndex =1) Then 'This is the column where I want to restrict input
       If dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value <> Nothing Then
            Dim value As String = dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            If Not Information.IsNumeric(value) Then
                MessageBox.Show("Invalid entry.")
                dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
                Exit Sub
            End If
        End If
     End If

End Sub

我遇到的问题是,如果我输入类似 3,3 或 4,2 的内容(其中 , 在数字之间或之后),它不会将其视为无效输入并且没有错误。但是,任何其他具有相同格式的特殊字符,+ 或 - 或 @ 都会被识别并显示错误消息。为什么这段代码没有将其识别为无效,我该如何包含该检查?谢谢你。

标签: vb.netvalidationinput

解决方案


在欧洲,逗号用于分隔单位和十分之一,例如英语使用点(在欧洲,10.3 = 十,十分之三 = 10,3),因此 VB 将其识别为有效数字。我会将您的条件更改为以下内容:

If (Not Information.IsNumeric(value)) OrElse value.Contains(",") Then

推荐阅读