Marked as Duplicate
Marked as Duplicate

Reputation: 1249

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;
    }
}

Upvotes: 2

Views: 1746

Answers (1)

Marked as Duplicate
Marked as Duplicate

Reputation: 1249

Looks like I only tried to refresh/update/invalidate the checkbox cell, not the DataGridView, and also I used the wrong methods.

Calling RefreshEdit on my DataGridView (myGridView.RefreshEdit()) updated the state of the checkbox.

I'm still not sure why exactly this even happened, but that fixes it.

Upvotes: 4

Related Questions