Skip to content

DataGridViewCheckBoxCell Always Returns ‘True’ for Value Property

Argh! This is why I avoid DataGridViews like the plague. I was working today in a Win Forms app attempting to perform a function if all checkboxes in my DataGridView column had been set to “true” (aka. the user has them all selected).

After much anguish and what I’m sure was a bug with the DataGridView, turns out to be by design. This is typical because I should have known there is always some hazy way of performing something with this control because it’s so freakin’ huge! Embarrassingly, when I see the property value I should have accessed it probably makes sense *grumble*.

Anyway, on to the goods, this is mainly a post for my Google searchers out there who also have had the same problem, if you are attempting to read the checkbox value from within a DataGridView Cell, specifically a DataGridViewCheckBoxCell and find that the Value property is constantly returning True even though you have unchecked it, you are accessing the wrong property.

The property you in fact need is EditedFormattedValue instead, as this will return the current (formatted) value of the cell regardless of if the cell has been committed (finished editing) or not.

Check the below sample on how this works by disabling a button if no checkboxes have been selected within a DataGridView:

        private void dataGridViewFiles_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on checkbox cells.
            if (e.ColumnIndex == ((DataGridView)sender).Columns[0].Index)
            {
                bool selected = false;
                foreach (DataGridViewRow row in ((DataGridView)sender).Rows)
                    selected = Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
                                   ? Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
                                   : selected;

                // Set button state based on if any checkboxes are selected.
                this.ButtonNext.Enabled = selected;
            }
        }

        private void dataGridViewFiles_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on checkbox cells.
            if (e.ColumnIndex == ((DataGridView)sender).Columns[0].Index)
            {
                bool selected = false;
                foreach (DataGridViewRow row in ((DataGridView)sender).Rows)
                    selected = Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
                                   ? Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
                                   : selected;

                // Set button state based on if any checkboxes are selected.
                this.ButtonNext.Enabled = selected;
            }
        }

Note! It is important you subscribe to the “CellContentDoubleClick” event as well to capture those checkbox clicks that occur on a double click! Otherwise the user can get the button in to a state where it is enabled without any checkboxes selected!

VN:F [1.9.10_1130]
Rating: 5.0/5 (4 votes cast)
VN:F [1.9.10_1130]
Rating: +1 (from 1 vote)
DataGridViewCheckBoxCell Always Returns 'True' for Value Property, 5.0 out of 5 based on 4 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

4 Comments

  1. Andrey Grishin

    Many thanks for this post! It was the only one on the first pages on google to help me sove this problem.

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 19-Jan-11 at 12:50 am | Permalink
  2. Devon Taig

    THANK YOU THANK YOU THANK YOU. Unfortunately, I did get a couple more gray hairs as a result of not stumbling upon your post quite quickly enough.

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 20-Apr-11 at 10:05 am | Permalink
  3. You, sir, are my Hero of the Day.

    Nobody ever talks about this damn property. THANKS!

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 20-Apr-11 at 10:51 am | Permalink
  4. Pablo Aliskevicius

    Thank you!
    I’d like to add: since the user can check/uncheck using the keyboard (space bar) and move around also using the keyboard (arrows, page up, page down), consider handling the CellLeave event instead of click events.
    JM2B ;-)

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 17-Aug-11 at 5:53 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...