Solved

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

Posted on 2014-03-02
3
2,589 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
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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