Solved

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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

634 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