问题描述
我需要一个方法来限制用户输入除 1
或 2
之外的任何内容我的dgv中的一列。我拥有它,所以用户被限制输入一个数字,但是我需要进一步限制,以满足 FOREIGN KEY
约束条件。
任何想法?
当前验证到位:
Private Sub dgvCategories_EditingControlShowing(ByVal sender As Object,_
ByVal e As DataGridViewEditingControlShowingEventArgs)_
处理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)_
句柄dgvCategories.DataError
如果StrComp(e.Exception.Message,输入字符串格式不正确)= 0然后
MessageBox。显示(请输入1(费用)或2(收入) e交易类型列)
'这将把数字改回原来的
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)处理dgvCategories.CellEnter
CurValue = dgvCategories.Rows.Item e.RowIndex).Cells(e.ColumnIndex).Value
End Sub
Private Sub dgvCategories_CellEndEdit(sender As Object,e As DataGridViewCellEventArgs)处理dgvCategories.CellEndEdit
Dim NewValue As Integer = dgvCategories。 Rows.Item(e.RowIndex).Cells(e.ColumnIndex).Value
如果NewValue< 1或NewValue> 2然后
dgvCategories.Rows.Item(e.RowIndex).Cells(e.ColumnIndex).Value = CurValue
End If
End Sub
我收到此错误:从字符串未分配转换为整数类型无效。
我假设的是从datagridview的New Row中的 NULL
条目。 / p>
任何猜测如何修改此解决方案?
编辑:感谢夜间我改变了我的逻辑:
私有函数CleanInputNumber(ByVal str As String)As String
返回系统。 Text.RegularExpressions.Regex.Replace(str,[03456789],1)
结束函数
私有子xDataGridView_CellValidating(ByVal sender As Object,ByVal e As System.Windows。 Forms.DataGridViewCellValidatingEventArgs)处理DataGridView1.CellValidating
如果DataGridView1.CurrentRow.Cells(ColumnType)。IsInEditMode n
Dim c As Control = DataGridView1.EditingControl
选择案例DataGridView1.Columns(e.ColumnIndex).Name
案例ColumnType
c.Text = CleanInputNumber(c.Text)
结束选择
End If
End Sub
像一个魅力一样工作
..我使用RegEx
与这个
私有函数CleanInputNumber ByVal str As String)As String
返回System.Text.RegularExpressions.Regex.Replace(str,[a-zA-Z\b\s-。],)
结束功能
这个
私有函数CleanInputAlphabet(ByVal str As String)As String
返回System.Text.RegularExpressions.Regex.Replace(str,[0-9\b\s-], )
结束功能
哟你应该弄清楚
这是我使用的事件
Private Sub xDataGridView_CellValidating(ByVal sender As Object,ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs)处理xDataGridView.CellValidating
如果xDataGridView.CurrentRow.Cells(xColumn)。IsInEditMode然后
Dim c As Control = xDataGridView.EditingControl
选择案例xDataGridView.Columns(e.ColumnIndex).Name
案例xColumn
c .Text = CleanInputNumber(c.Text)
'CaseyColumn
'c.Text = CleanInputAlphabet(c.Text)
结束选择
结束如果
End Sub
I need to come up with a way to restrict the user from entering anything other than 1
or 2
in a column in my dgv. I have it so the user is restricted to entering a number, but I need further restrictions to meet the FOREIGN KEY
constraints.
Any ideas?
Current Validation in place:
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
Playing around with solutions:
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
I get this error: Conversion from string "Unassigned" to type 'Integer' is not valid.
Which I'm assuming is from the NULL
entries in the "New Row" of the datagridview.
Any guesses on how to modify this solution?
EDIT: Thanks to Nocturnal, I changed my logic to this:
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
Works like a charm!
many different ways to approach this .. i use RegEx
with this
Private Function CleanInputNumber(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[a-zA-Z\b\s-.]", "")
End Function
and this
Private Function CleanInputAlphabet(ByVal str As String) As String
Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "")
End Function
you should figure it out
and this is the event i use
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
这篇关于如何将用户输入限制为DataGridView中的几个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!