Solved

Allow only positive integer values in a certain Column of DataGridView?

Posted on 2009-05-07
5
630 Views
Last Modified: 2012-05-11
Hello,

does someone have me please a little  code snippet, how I can limit the user input in the DataGridView_CellValidating Event to only positive integer values?
0
Comment
Question by:MSFanboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24323913
Something like this?

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    int value = 0;
    int.TryParse(e.FormattedValue.ToString(), out value);
    if (value > 0)
        e.Cancel = false;
    else
        e.Cancel = true;
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24323925
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:

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    int value = 0;
    int.TryParse(e.FormattedValue.ToString(), out value);
    e.Cancel = value <= 0;
}

Open in new window

0
 
LVL 3

Expert Comment

by:Kevin Robinson
ID: 24358974
Try http://www.componentfactory.com/ FREE toolkit.  It has a tool for this.

0
 

Accepted Solution

by:
MSFanboy earned 0 total points
ID: 24371879
I already use Krypton`s Grid :P and solved it that way see code below

@abel

thanks for your tips, but they did not help me in my specific case.



private void articlesDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = false;  
            MessageBox.Show("Nur Zahlen größer 0 sind erlaubt", "Fehler");
        }
 
translation: Only NUmbers larger than 0 are allowed!

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24372431
Glad you found a solution, but why then did you ask "how I can limit the user input in the DataGridView_CellValidating event..."? If you want to diverge from your original q. please inform the involved experts in the thread.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question