Solved

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

Posted on 2009-05-07
5
607 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
Comment Utility
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
Comment Utility
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
Comment Utility
Try http://www.componentfactory.com/ FREE toolkit.  It has a tool for this.

0
 

Accepted Solution

by:
MSFanboy earned 0 total points
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

11 Experts available now in Live!

Get 1:1 Help Now