Sev09
Sev09

Reputation: 883

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

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!

Upvotes: 2

Views: 2314

Answers (1)

Nocturnal
Nocturnal

Reputation: 386

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

Upvotes: 2

Related Questions