Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-02
3
Medium Priority
?
3,084 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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