Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a windows forms program that uses a datagridview. I'm trying to find a way to prevent a user from entering special characters (e.g. $,@,!,#,%,^,&) in my datagridview. When the user inputs a special character I have an approprioate message box appear explaining their mistake, then I provide them a default value. I have everything working except a way to prevent the special character or symbols. I'm thinking something like this has to work, but I can't seem to find any way of preventing this sort of entry:

If (columnindex = 0) Then 'checking value for column 1 only
            Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
            If cellString String.IsSymbol(cellString) = true Then

                    MessageBox.Show("Special Characters Not Allowed")
                End If
                DataGridView1.Rows(rowindex).Cells(columnindex).value = "Default Value"
                Exit Sub
            End If

1 solution

if you want that user can not insert any special char then handle it in cell_keypress event
and if e.key is special char then handle it using
e.handle = true;

Happy Coding!
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