Solved

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

Posted on 2014-03-02
3
2,693 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
[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 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 39899896
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
ID: 39900036
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
ID: 39904970
Thanks Carl and Naman.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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