MSFanboy
asked on
Allow only positive integer values in a certain Column of DataGridView?
Hello,
does someone have me please a little code snippet, how I can limit the user input in the DataGridView_CellValidatin g Event to only positive integer values?
does someone have me please a little code snippet, how I can limit the user input in the DataGridView_CellValidatin
If you mean strictly positive, the code above works. If you mean to include zero, the code ">" needs to be replaced with ">=".
You can use a shortcut for the if/else. I wrote it out to show what is going on, but if you are familiar with booleans and if you like short code, you can do this as well:
You can use a shortcut for the if/else. I wrote it out to show what is going on, but if you are familiar with booleans and if you like short code, you can do this as well:
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int value = 0;
int.TryParse(e.FormattedValue.ToString(), out value);
e.Cancel = value <= 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad you found a solution, but why then did you ask "how I can limit the user input in the DataGridView_CellValidatin g event..."? If you want to diverge from your original q. please inform the involved experts in the thread.
Open in new window