?
Solved

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

Posted on 2014-03-02
3
Medium Priority
?
2,941 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 1200 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 800 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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