简体   繁体   中英

How to set the CheckBox column in DataGridView value to true

GD All,

I've looking around for a solution to my below challenge.

I have got a form with an unbound datagridview, the dg has one added column that allows user to select a method to be used. The state of the event is stored in a database and after re-opening the form the code checks if the event is in an 'open' state, if so it compares the previously selected method with the methods in the datagrid and should set the previously activated method to be the 'selected' method.

Yet I can't seem to get this to work unfortunately...

The below code loops through the methods in the dg and compares the values, if it meets the methodID it should set the value to 'True' or to the TrueValue anyway.

This is initialized if the database check returns true and after full initialisation of the form, where session.methodID is a field in the returned LINQ query.

For Each r As DataGridViewRow In dgMethods.Rows

   If r.Cells(1).Value = session.methodID Then
      Dim c As DataGridViewCheckBoxCell = r.Cells(0)
      c.Value = c.TrueValue
   End If

Next

Unfortunately, this doesn't set the checkbox to 'Checked'. The loop runs and evaluates the comparison between r.Cells(1).Value and session.methodID correct and triggers correctly.

The interesting thing is if I do a similar loop after the 'CellContentClick' event it does do exactly what is expected. (the example below sets all checkbox values to checked)

Private Sub dgMethods_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgMethods.CellContentClick

    'Only single selection allowed, so clear table before submitting new selection
    For Each r As DataGridViewRow In dgMethods.Rows
        Dim c As DataGridViewCheckBoxCell = r.Cells(0)
        c.Value = c.TrueValue

    Next

    dgMethods.CommitEdit(DataGridViewDataErrorContexts.Commit)

End Sub

So, apparently there is a difference in the state between just calling the loop on the dgMethods and when the dgMethods.CellContentClick event has triggered, yet I do not know which one ? There are many many post on trying to set the CheckBox column yet I have not been able to get any of them working.

Anybody have any idea ?

I would appreciate your suggestions ?

I was not sure of being undestand your question... but there's s simple way to check and change the state of a chechbox cell in a datagridview:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each dr As DataGridViewRow In DataGridView1.Rows
            If CBool(dr.Cells(0).Value) = True Then dr.Cells(0).Value = False : Continue For
            If CBool(dr.Cells(0).Value) = False Then dr.Cells(0).Value = True
        Next

    End Sub

In this example, when you click this button for each row in the datagridview, checks the checkboxcell and set the value to FALSE or TRUE depending of his value.

Hope this helps you.

And let me one more tip. If you get acces to the cells for his name instead of his index use his name, it should helps you avoiding troubles ;)

GD All,

After searching further I came across the following interesting behaviour.

The method selection process is part of a form called 'frmAddEvent', the frmAddEvent form is called from a main form using below routine.

The new form instance is created and afterwards filled using a public sub in the form class called InitializeForm() which uses a GUID parameter to retrieve corresponding data to set the form fields.

If Not (isOpened(rsTankName.unqID)) Then
   Dim newForm As New frmAddEvent() '(rsTankName)
   newForm.InitializeForm(rsTankName)
   newForm.Show()

Else

End If

The initialization sub queries several datatables and sets the corresponding fields in the new form instance correctly if applicable. Part of that setting is the method selection in the dgMethods datagridview.

It would appear that the sequence in which you call the form makes all the difference as the below code works perfectly:

If Not (isOpened(rsTankName.unqID)) Then
   Dim newForm As New frmAddEvent() '(rsTankName)

   newForm.Show()
   newForm.InitializeForm(rsTankName)
Else

End If

So calling the newForm.InitializeForm(rsTankName) after the newForm.Show event allows the datagridview to set the CheckBoxColumn correctly.

Likely because the actual CheckBox itself is only actually generated upon the Show command, despite the fact that it is 'available' as a cell with DataGridViewCheckBoxColumn properties in the datagrid, directly after the New frmAddEvent has created the new form instance. The actual CheckBox and its corresponding CheckedState is not created before the newForm.Show event is called. It would appear that the when the CheckBox is created for display (during the newForm.Show event) there is no comparison made to its actual value.

So, in order to set the Checkbox column on initiating a new form you have to call the Show event prior to setting the DataGridViewCheckBoxColumn values otherwise the CheckBox will not show it as 'Checked'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM