How to restrict user input to a couple values in a DataGridView

后端 未结 1 1661
执念已碎
执念已碎 2021-01-22 00:39

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

相关标签:
1条回答
  • 2021-01-22 01:15

    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
    
    0 讨论(0)
提交回复
热议问题