Guys how do I set a column of a datagrid so that it will only accept numbers? I have this column labeled "Grade" which of course, should not accept letters. I have done setting its Format property but it is not working... Please

6 Years
Discussion Span
Last Post by Mitja Bonca

You can use the IsNumeric boolean property

If(IsNumeric(...)) Then
msgbox("is a number")
End If

Edited by abhay1234: n/a


YOu have to use CellValidating event:

Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
    ByVal e _
    As DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Me.dataGridView1.Rows(e.RowIndex).ErrorText = ""
    Dim newInteger As Integer

    ' Don't try to validate the 'new row' until finished 
    ' editing since there
    ' is not any point in validating its initial value.
    If dataGridView1.Rows(e.RowIndex).IsNewRow Then Return
    If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
        OrElse newInteger < 0 Then

        e.Cancel = True
        Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"

    End If
End Sub

For more info check here.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.