vb.net - 验证数字数据输入时控制数据网格视图的逗号输入
问题描述
我有一个数据网格视图,我必须将其中一列的输入限制为数字输入(整数和小数位)。用户将值直接输入到数据网格视图中。总共有 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 的内容(其中 , 在数字之间或之后),它不会将其视为无效输入并且没有错误。但是,任何其他具有相同格式的特殊字符,+ 或 - 或 @ 都会被识别并显示错误消息。为什么这段代码没有将其识别为无效,我该如何包含该检查?谢谢你。
解决方案
在欧洲,逗号用于分隔单位和十分之一,例如英语使用点(在欧洲,10.3 = 十,十分之三 = 10,3),因此 VB 将其识别为有效数字。我会将您的条件更改为以下内容:
If (Not Information.IsNumeric(value)) OrElse value.Contains(",") Then
推荐阅读
- javascript - 我该怎么做才能返回一个里面有 dict 的数组?
- metatrader4 - 有人可以为 MT4 Manager API 帐户创建、存款/取款提供 REST api 或 powershell 脚本吗?
- sql - 每天的第一个和最后一个交易日期
- git - 将 Azure Synapse 数据库导入 Visual Studio 时出错
- c++ - 只接受枚举类型的函数
- xml - XML 缩进字符 (xmllint)
- vector - 如何计算3D平面中给定的三角形顶点的法线向量
- c++ - 将字符串分配给字符串返回类型函数而不复制
- twitter-bootstrap - 具有灵活导航栏的网页
- mysql - 如何在 typeorm 中使用 .env 连接到数据库