Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm hoping to find out a way to restrict a user from entering any non-numeric input into my datagridviewcolumn. Also I have already restricted the user from entering any negative numbers and from leaving the cell blank. If anyone can find a way to restrict the user from entering letters and non-numeric input I would greatly appreciate it!

VB
If (e.ColumnIndex = 8) Then 'This specifies the column number
        Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            MessageBox.Show("Cannot Be Empty")
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0
        ElseIf cellData < 0 Then
            MessageBox.Show("Negatives Values Not Allowed")
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0

            Exit Sub

        End If
    End If
Posted

1 solution

Try regular expression.
I think a function like this would work to give you a true/false if the value in the column is valid
It will only allows numbers.

C#
private static bool IsNumeric(string cellData)
        {
            System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("^[0-9]*");
            return rx.IsMatch(cellData);
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900