vendredi 29 juin 2018

DataGridView checkbox doesn't update until another control is clicked on

I implemented a header checkbox (from this answer) for my DataGridView that checks/unchecks all the checkboxes. It works for all checkboxes other than the first one. The first checkbox will only update its state after another control has been clicked on, as seen here:

GIF

And even later I noticed that the checkbox that doesn't update its state is the last checkbox that has been manually clicked on.

enter image description here

To be honest I'm not even exactly sure what's going on. What I tried though was creating an invisible dummy button and PerformClick() it, hoping that it would count as a click on a control and would update the state of the checkbox.

I also looked into Refresh(), Update(), and Invalidate(), but the checkbox cell doesn't have those methods and I couldn't do it.

This function is launched when the header checkbox is checked/unchecked:

private bool selectAllChecked = false;
private void SelectAll(object sender, EventArgs e) {
    selectAllChecked = !selectAllChecked;

    foreach (DataGridViewRow row in myGridView.Rows) {
        DataGridViewCheckBoxCell checkb = (DataGridViewCheckBoxCell)row.Cells["Checkbox"];
        checkb.Value = selectAllChecked;
    }
}




Aucun commentaire:

Enregistrer un commentaire