stackexchange12
stackexchange12

Reputation: 652

Is there a RangeValidator in VB.net?

I'm looking for an easy way to specify allowed inputs for my datagridview. I ran across something called a "RangeValidator" on MSDN but it appears to only be for web development in ASP.NET. Is there an alternative to this for VB.NET 4.5 using Visual Studio 2012? My goal is to have a datagridviewcell that allows only integers >= zero, and no fractional inputs. I appreciate any suggestions and help anyone can give. :)

Upvotes: 0

Views: 456

Answers (1)

David -
David -

Reputation: 2031

I found the below code here http://vbcity.com/forums/t/152435.aspx i think it may help.

Private Sub DataGridView1_EditingControlShowing1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If DataGridView1.CurrentCell.ColumnIndex = 1 Then
        Dim txtedit As TextBox = DirectCast(e.Control, TextBox)
        AddHandler txtedit.KeyPress, AddressOf txtEdit_KeyPress
    End If
End Sub

Private Sub txtEdit_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    If DataGridView1.CurrentCell.ColumnIndex = 1 Then
        If ("0123456789\b".IndexOf(e.KeyChar) = -1) Then
            If e.KeyChar <> Convert.ToChar(Keys.Back) Then
                e.Handled = True
            End If
        End If
    End If
End Sub

Upvotes: 1

Related Questions