Solved

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

Posted on 2009-05-07
5
619 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now