Results 1 to 16 of 16

Thread: Datagridview Checkbox Cell won't check

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Datagridview Checkbox Cell won't check

    I have a Windows Form that contains a Datagridview control. This control has 2 columns, one is a DataGridViewCheckBoxCell (which is checked by default) and the other is a TextBox cell. If DGV contains multiple rows, I can check/uncheck the CheckBox for all rows except the first row (0 index). I don't understand why this happens for the first row only. Below is the code I have for this DGV.

    Code:
        Private Sub dgvSN_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvSN.CellMouseUp
            'Debug.Print("dgvSN_CellMouseUp")
    
            Try
                EH.ErrorMessage = String.Empty
    
                If e.RowIndex > -1 And e.ColumnIndex = 0 Then
                    Dim cboSelected As DataGridViewCheckBoxCell = CType(dgvSN.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell)
    
                    If CBool(cboSelected.Value) = True Then
                        cboSelected.Value = False            'Even when this line is executed, the checkbox WON'T uncheck for the very first row
                    Else
                        cboSelected.Value = True
                    End If
                End If
    
            Catch ex As Exception
                EH.ErrorMessage = "frmCertificates/dgvSN_CellMouseUp() - " & ex.Message & "~E"
            End Try
    
            EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
        End Sub
    Blake

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview Checkbox Cell won't check

    What exactly is that code supposed to be achieving from the user's perspective?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Datagridview Checkbox Cell won't check

    After commenting out that code....NOTHING! It does absolutely nothing. However, I still can't check/uncheck the very first line. The rest of the lines...no problem!

    I did notice something though. If the first row was checked, which it is by default, and I tried to uncheck it...it would do anything. However, as soon as I checked/unchecked another CB, the first one would become unchecked. Then, I would try and check the first CB again and nothing happened until I checked/unchecked another one. Only then would the first CB become checked.
    Last edited by blakemckenna; Apr 28th, 2016 at 08:14 AM.
    Blake

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Datagridview Checkbox Cell won't check

    That wasn't the question. The question was: From the user's point of view, what is SUPPOSED to happen? I'm going to go out on a limb and say that what is supposed to happen is that the checkbox should be able to check/uncheck.

    If the field is a DataGridViewCheckBoxCell why are you handling it?

    And of course I'm going to go with the stupid question and ask, have you set breakpoints and stepped through the code to make sure it's running the way you think it should be?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Datagridview Checkbox Cell won't check

    I have code for what is to happen when the checkbox is CHECKED. No point in putting it in until I figure out why the first row checkbox won't check/uncheck when clicked on. I have stepped through the logic I provide and it should check/uncheck just like the rest of the rows. It's the first row only that is acting this way.
    Blake

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview Checkbox Cell won't check

    Quote Originally Posted by techgnome View Post
    If the field is a DataGridViewCheckBoxCell why are you handling it?
    Exactly. You don't have to do anything to be able to check and uncheck the box in a DataGridViewCheckBoxCell so, if it doesn't work that way then either something is corrupt or else some other code is interfering with the default behaviour. If we know what that code is supposed to be doing then we can hopefully determine why it's having the undesired effect. If the code originally posted can be removed without effect and the unexpected behaviour persists then it must be some other grid event handler that's causing it. I'd be inclined to comment out all such methods and then add them back in one by one until the behaviour returns.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Datagridview Checkbox Cell won't check

    When the code is working as it should, it will make a call to the database and retrieve data to populate another DGV. I guess my next question would be....which Event should I use in order to facilitate this data retrieval?
    Blake

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Datagridview Checkbox Cell won't check

    Check out my MSDN code sample that may very well assist you with your current task. There are no direct answer here but information via projects to learn from.

    Basics to advance working with DataGridView Checkbox columns

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Datagridview Checkbox Cell won't check

    Well, mouse up isn't what I would have used, but I think I found the issue...
    I used the CellContentClick ... I think it's a bit more stable than mouseup...
    but what's happening is the cell is in edit mode, so it doesn't initially hold the new value... you have to stop editing in order for the value to commit.

    In an empty project, add a DataGridView, add two cols, the first being a checked cell and the second being text, and add this codE:
    Code:
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If e.ColumnIndex = 0 Then
                Dim cboSelected As DataGridViewCheckBoxCell = CType(DataGridView1.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell)
                MessageBox.Show(cboSelected.Value)
            End If
        End Sub
    when you run it, and click the first check cell... you'll get a blank messagebox, but the cell will look checked... dimiss the messgebox, click the checkbox again. The check mark will clear but the messagebox will read True.

    Now make this one line change:
    Code:
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If e.ColumnIndex = 0 Then
                Dim cboSelected As DataGridViewCheckBoxCell = CType(DataGridView1.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell)
                DataGridView1.EndEdit()
                MessageBox.Show(cboSelected.Value)
            End If
        End Sub
    Run the same test... now you'll see the True/False in synch with the checkmark in the UI.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Datagridview Checkbox Cell won't check

    I have attached a mp4 video showing my actions. Currently, I have NO Events coded for the DGV...
    Blake

  11. #11
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Datagridview Checkbox Cell won't check

    Quote Originally Posted by blakemckenna View Post
    I have code for what is to happen when the checkbox is CHECKED. ...
    Quote Originally Posted by blakemckenna View Post
    When the code is working as it should, it will make a call to the database and retrieve data to populate another DGV. I guess my next question would be....which Event should I use in order to facilitate this data retrieval?
    The DGV's events can be a bit confusing. I believe what you want is the CurrentCellDirtyStateChanged event.

    VB.Net Code:
    1. Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    2.     Dim cell As DataGridViewCheckBoxCell = TryCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)
    3.  
    4.     If cell IsNot Nothing AndAlso cell.ColumnIndex = 0 Then
    5.         DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    6.         If CBool(cell.Value) Then Beep()
    7.     End If
    8. End Sub

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Datagridview Checkbox Cell won't check

    TnTinMN,

    Below is the code that I am using and still no changes. I don't understand why this is happening. I've also attached a video.

    Code:
        Private Sub dgvSN_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgvSN.CurrentCellDirtyStateChanged
            'Debug.Print("dgvSN_CurrentCellDirtyStateChanged")
    
            Try
                Dim cell As DataGridViewCheckBoxCell = TryCast(dgvSN.CurrentCell, DataGridViewCheckBoxCell)
    
                If cell IsNot Nothing AndAlso cell.ColumnIndex = 0 Then
                    dgvSN.CommitEdit(DataGridViewDataErrorContexts.Commit)
    
                    If CBool(cell.Value) <> False Then
                        cell.Value = False
                    Else
                        cell.Value = True
                    End If
                End If
    
            Catch ex As Exception
                EH.ErrorMessage = "frmCertificates/dgvSN_CurrentCellDirtyStateChanged() - " & ex.Message & "~E"
            End Try
    
            EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
        End Sub
    Blake

  13. #13
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: Datagridview Checkbox Cell won't check

    Here's one of my bookmarked link on DataGridViewCheckBoxCell event.

    VBForum DataGridViewCell question
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  14. #14
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Datagridview Checkbox Cell won't check

    What is the point the purpose of this code?
    Quote Originally Posted by blakemckenna View Post
    Code:
                    If CBool(cell.Value) <> False Then
                        cell.Value = False
                    Else
                        cell.Value = True
                    End If
    If your goal is to prevent changes, then that code might have relevance if the changes were committed by executing a second CommitEdit(DataGridViewDataErrorContexts.Commit) statement.

    The CurrentCellDirtyStateChanged event will fire after as the cell value changes. The CommitEdit(DataGridViewDataErrorContexts.Commit) method will tell the DGV to accept the change as the current value.

    If your goal is to respond immediately to changes based on the cell value, then:
    Code:
    If CBool(cell.Value) Then ' note that you should not test if a boolean = True/False, i.e.: CBool(cell.Value) = True
       ' execute code for when true
    Else
        ' execute code for when false
    End If
    Do not try to set the cell value in code.

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Datagridview Checkbox Cell won't check

    unless your intent is to change the checkmark setting, stop doing so... I think that's part of the problem. you're handling an event and then changing it and wondering why it doesn't work. you would actually be better off with a message box or some other action that isn't mucking about with the data you're trying to process. at the moment the code will uncheck a checked box... or check the box you just unchecked... the net effect is nothing... no change... or a checkbox that is basically locked & readonly. if that's the intent, there's better ways of handling that. But your previous comments about only wanting to do an action if it is checked says that's not what you're after... OK... fine... then DO something when it is checked but stop mucking with the data and setting which is undoing what hte check box wants to do naturally. display a message box. call your calculations you want to do, write to the debug console windopw, DO SOMETHING but STOP unsetting/setting the check box.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Datagridview Checkbox Cell won't check

    Well...I found a direct correlation to the problem. Initially, I had the ColumnHeadersVisible set to False. When I changed it back to True (Default setting), it worked! I was able to check/uncheck the first row. Not sure if that is a bug or not...seems to me it is. My work-around was to set the ColumnHeaderDefaultCellStyle FontSize to 1 and clear out the Column.HeaderText property. Everything works as it should with just a small hint of the Column Headers.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width