dimanche 17 avril 2016

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 ?

Aucun commentaire:

Enregistrer un commentaire