checkbox in datagridview

jramos

Member
Joined
Apr 2, 2013
Messages
5
Location
Lisboa
Programming Experience
10+
Hello,
I've looked for this subject and found various examples but can't put them to work.
So, i need help to understand what i'm doing wrong.

I have a datagridview, with 9 columns. The 1st column is a checkbox. Then i have 2 buttons, one disabled and only want to enable it when the user check's one column.
If the user check's more than one checkbox, the button stays disabled.

The code i have to see if a checkbox is checked is the following:

Private Sub DGVSites_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DGVSites.CellContentClick
        CntCheck = 0
        'Count the number of rows checked
        For Each Row As DataGridViewRow In DGVSites.Rows
            If Row.Cells("DGVChkBox").Value = True Then
                CntCheck += 1
            End If
        Next
        'S? tornamos o bot?o de alterar visivel caso haja s? uma linha selecionada
        If CntCheck = 1 Then
            BtnAlterar.Visible = True
            DGVSites.Refresh()
        Else
            BtnAlterar.Visible = False
            DGVSites.Refresh()
        End If
End Sub


Thanks
 
The Value of the cell doesn't change when you click on the cell content. The change is only committed when the user presses Enter or navigates to another cell. With that in mind, you should probably be handling ValueChanged instead of CellContentClicked.

If you want the change to be committed immediately then you have to do that yourself. The documentation for the CellContentClick event demonstrates how to do that.

As an aside, let's clean up this code:
        If CntCheck = 1 Then
            BtnAlterar.Visible = True
            DGVSites.Refresh()
        Else
            BtnAlterar.Visible = False
            DGVSites.Refresh()
        End If
Firstly, you should almost never have any code that appears in both the If and the Else blocks. If you're going to call Refresh on your grid whether the tested condition is True or False then it doesn't need to be in either block. It's going to happen no matter what so you put it where you put other code that happens no matter what, i.e. outside both blocks:
        If CntCheck = 1 Then
            BtnAlterar.Visible = True
        Else
            BtnAlterar.Visible = False
        End If

        DGVSites.Refresh()
That said, you shouldn't need to call Refresh anyway, so you can probably just get rid of that line. Refresh simply redraws the control. It has nothing to do with data. The grid will redraw itself whenever it's appropriate to do so.

Also, if you want to set a Boolean field/property based on the value of another Boolean expression then there's no need for an If...Else at all. You simply assign one to the other. In this case:
BtnAlterar.Visible = (CntCheck = 1)
If you need to negate the value then you can do that like this:
BtnAlterar.Visible = (CntCheck <> 1)
or this:
BtnAlterar.Visible = Not (CntCheck = 1)
 
Back
Top