Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I'm using a datagridview populated with excel data using the oledataadapter. I'm trying to find a way to prevent the user from entering negative numbers as well as fractional numbers. Is there an easy way to prevent this? I greatly appreciate any help or suggestions anyone can give.
Posted

1 solution

Masked Edit Column[^]

You can use that component to keep your user from entering values that you don't want.

The only other way is to catch the EditingControlShowing event, cast the editing control to a text box, then capture the KeyPressEvent. Then use typical keypress filtering code to not allow the user to enter a "-" or "." (or anything non-numeric) in the text box.
 
Share this answer
 
v2
Comments
CAS1224 24-Jun-13 13:42pm    
This will only work for a textbox though, right?
Ron Beyer 24-Jun-13 19:34pm    
I'm sure that the method can be extended to validate any type of editing control in the data grid view, you just have to check what the type of the editor control is in the EditControlShowing event, for example:

if (e.Control is Textbox)
//Textbox validation
else if (e.Control is Combobox)
//Combobox validation

etc...

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