Solved

How to set min/max value in DatagridviewTextBoxColumn (in Winform)

Posted on 2014-03-02
3
2,359 Views
Last Modified: 2014-03-04
Hi expert
Is it possible to set the min/max values in DataGridViewTextBoxColumn?
I want it to be between 10-1000.

I am using Winform .NET 3.5, C#.

Thanks in advance.
0
Comment
Question by:dominicwong
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
Comment Utility
You would have to either add a column to your grid with a custom control that enforces a range, or you can handle the CellValidating event and check the value manually. Something along the lines of:
        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
            dataGridView1.Rows.Add();

            dataGridView1.CellValidating += dataGridView1_CellValidating;
        }

        void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == <index of your column>)
            {
                int val;
                if (int.TryParse(e.FormattedValue.ToString(), out val))
                {
                    if (val < 10 || val > 1000)
                        dataGridView1.Rows[e.RowIndex].ErrorText = "Number must be between 10 and 1000";
                }
                else
                {
                     dataGridView1.Rows[e.RowIndex].ErrorText = "Value must be numeric";
                }
            }
        }

Open in new window

0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 200 total points
Comment Utility
Yes you can use CellValidating event for the same:

Here my column Index is 2.

   private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                int cellValue = int.Parse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
                if (!(cellValue >= 10 && cellValue <= 100))
                {
                    e.Cancel = true;
                }
            }
        }

Open in new window


http://social.msdn.microsoft.com/Forums/windows/en-US/e36c8618-6224-49e3-af36-b536bd2a5ffb/datagridview-cell-validation?forum=winformsdatacontrols
0
 

Author Closing Comment

by:dominicwong
Comment Utility
Thanks Carl and Naman.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

9 Experts available now in Live!

Get 1:1 Help Now