我需要想出一种方法来限制用户在我的dgv中的列中输入1
或2
以外的任何内容。我有它所以用户只能输入一个数字,但我需要进一步的限制来满足FOREIGN KEY
约束。
有什么想法吗?
当前验证:
Private Sub dgvCategories_EditingControlShowing(ByVal sender As Object, _
ByVal e As DataGridViewEditingControlShowingEventArgs) _
Handles dgvCategories.EditingControlShowing
CType(Me.dgvCategories.Columns(0), DataGridViewTextBoxColumn).MaxInputLength = 50
CType(Me.dgvCategories.Columns(1), DataGridViewTextBoxColumn).MaxInputLength = 1
End Sub
Private Sub dgvCategories_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) _
Handles dgvCategories.DataError
If StrComp(e.Exception.Message, "Input string was not in a correct format.") = 0 Then
MessageBox.Show("Please Enter either 1 (Expense) or 2 (Income) in the Transaction Type column.")
'This will change the number back to original
dgvCategories.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = " "
End If
End Sub
使用解决方案:
Dim CurValue As Integer
Private Sub dgvCategories_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvCategories.CellEnter
CurValue = dgvCategories.Rows.Item(e.RowIndex).Cells(e.ColumnIndex).Value
End Sub
Private Sub dgvCategories_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvCategories.CellEndEdit
Dim NewValue As Integer = dgvCategories.Rows.Item(e.RowIndex).Cells(e.ColumnIndex).Value
If NewValue < 1 Or NewValue > 2 Then
dgvCategories.Rows.Item(e.RowIndex).Cells(e.ColumnIndex).Value = CurValue
End If
End Sub
我收到此错误:Conversion from string "Unassigned" to type 'Integer' is not valid.
我假设的是来自datagridview的“新行”中的NULL
条目。
有关如何修改此解决方案的任何猜测?
编辑:感谢Nocturnal,我改变了我的逻辑:
Private Function CleanInputNumber(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[03456789]", "1")
End Function
Private Sub xDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If DataGridView1.CurrentRow.Cells("ColumnType").IsInEditMode Then
Dim c As Control = DataGridView1.EditingControl
Select Case DataGridView1.Columns(e.ColumnIndex).Name
Case "ColumnType"
c.Text = CleanInputNumber(c.Text)
End Select
End If
End Sub
像魅力一样!
答案 0 :(得分:2)
采用RegEx
的方法很多用这个
Private Function CleanInputNumber(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[a-zA-Z\b\s-.]", "")
End Function
和这个
Private Function CleanInputAlphabet(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "")
End Function
你应该弄清楚
这是我使用的事件
Private Sub xDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles xDataGridView.CellValidating
If xDataGridView.CurrentRow.Cells("xColumn").IsInEditMode Then
Dim c As Control = xDataGridView.EditingControl
Select Case xDataGridView.Columns(e.ColumnIndex).Name
Case "xColumn"
c.Text = CleanInputNumber(c.Text)
'Case "yColumn"
' c.Text = CleanInputAlphabet(c.Text)
End Select
End If
End Sub