Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-07
5
Medium Priority
?
675 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

927 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