gpt4 book ai didi

vb.net - DataGridView 中整数的输入验证

转载 作者:行者123 更新时间:2023-12-02 05:00:25 25 4
gpt4 key购买 nike

我可以使用下面的代码来检查用户是否在 datagridview 单元格中输入了字符串。如果用户输入字符串,则会弹出一条消息,告诉他们“只允许输入数字”。这正是我希望我的代码执行的操作。但是,如果我尝试在用数字填充的数据列中使用此代码,我会收到一条错误消息,指出“发生错误,正在解析提交”。如果有人能够弄清楚这里的问题是什么,我将不胜感激!

If (e.ColumnIndex = 3) Then 'checking numeric value for column 3 only
Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
For Each c As Char In value
If Not Char.IsDigit(c) Then
MessageBox.Show("Please Enter numeric Value")
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 3 'This will set the defaultvalue of the datagrid cell in question to the value of "3"
Exit Sub
End If
Next
End If

最佳答案

此解决方案不仅检查非整数值,它还适用于整个数据 GridView 中填充有数字的每一列。此外,如果用户输入非整数,则将为 datagridviewcell 提供默认值,此默认值是先前的数字。

Private Sub DataGridView1_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError

If StrComp(e.Exception.Message, "Input string was not in a correct format.") = 0 Then
MessageBox.Show("Please Enter a numeric Value")
'This will change the number back to original
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = " "
End If




End Sub

关于vb.net - DataGridView 中整数的输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17180310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com