程式:
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
'DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
End Sub
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If e.ColumnIndex >= 10 AndAlso e.ColumnIndex <= 19 Then
Exit Sub
End If
Dim newInteger As Integer
Dim newDeciaml As Decimal
Dim grid As DataGridView = CType(sender, DataGridView)
grid.Rows(e.RowIndex).ErrorText = ""
If String.IsNullOrEmpty(e.FormattedValue.ToString) Then
DataGridView1.Rows(e.RowIndex).ErrorText = _
"不允許空白!!"
MsgBox("不允許空白")
e.Cancel = True
Else If grid.Columns(e.ColumnIndex).Name = "新增次數" OrElse grid.Columns(e.ColumnIndex).Name = "序號" _
OrElse grid.Columns(e.ColumnIndex).Name = "項次" Then
If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
OrElse newInteger < 0 Then
DataGridView1.Rows(e.RowIndex).ErrorText = _
"只允許Integer!!"
MsgBox("只允許Integer")
e.Cancel = True
End If
ElseIf grid.Columns(e.ColumnIndex).Name = "金額" Then
If Not Decimal.TryParse(e.FormattedValue.ToString(), newDeciaml) Then
DataGridView1.Rows(e.RowIndex).ErrorText = _
"只允許Decimal!!"
MsgBox("只允許Decimal")
e.Cancel = True
End If
End If
End Sub
參考:
DataGridView: why is CellValidating firing as the grid is filling?
DataGridView.CellValidating 事件,確定使用者只輸入正整數。
DataGridView中数据输入验证
c# DataGridView中的单元格中只能输入数字的解决方法
C#如何在DataGridView控件中验证数据输入
DataGridViews, DataBinding and non validating cell values
[C#]DataGridView欄位驗證只能是數字且不能為空白
WinForm中DataGridView验证单元格输入的是数字
[ADO.NET][WinForm] 限制 DataGridView 輸入字元
DataGridView 控制項 (Windows Form)
判斷是否為數字、整數的正則表達式方法
C#检测字符串是否为正整数
沒有留言:
張貼留言