DataGridViewCheckBoxCell how to show forms marked at installation during loading

I have a DataGridView that loads data from a DataTable along with an unrelated DataGridViewCheckBoxCells column. Rows in the DataGridView are compared with a single DataTable with the values ​​stored by the user, and if there is a match, the check box for this row should be checked.

Here is the code that compares the values ​​and sets the checkbox value to true:

foreach (int j in selectedObjectives) { foreach (DataGridViewRow r in dgvObjectives.Rows) { if (j == Convert.ToInt32(r.Cells["ObjectiveID"].Value)) { dgvObjectives.CurrentCell = r.Cells["Select"]; ((DataGridViewCheckBoxCell)r.Cells["Select"]).Value = true; //dgvObjectives.InvalidateCell(r.Cells["Select"]); //dgvObjectives.EndEdit(); //dgvObjectives.CommitEdit(DataGridViewDataErrorContexts.Commit); } if (Convert.ToInt32(r.Cells["ObjectiveID"].Value) == selectedIndex) { r.Selected = true; } } } 

When I call the method to perform this action while loading the private void WQMDrill_Load(object sender, EventArgs e) form, the values ​​are set correctly, but the checkboxes are not checked. However, when called after the form has finished loading, the code works fine. Unfortunately, for me it is absolutely necessary to verify during the boot process.

I hope that with my problem it was clear that any help on this issue would be greatly appreciated. As you can see, I tried to invalidate only the cell, as well as the entire DataGridView control. I also have

 private void dgvObjectives_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (this.dgvObjectives.CurrentCell.ColumnIndex == 0) { this.dgvObjectives.CommitEdit(DataGridViewDataErrorContexts.Commit); } } 

This does not work during this time. Thanks.

+4
source share
3 answers

You can put your choice of checkbox and update logic in the DataBindingComplete event handler, this works after FormLoad, but before anything is displayed to the user.

+1
source

I'm not sure if calling CommitEdit will cause Paint to shell in the cell. Try to handle the CellMouseUp event and fire EndEdit if the column is a checkbox column.

 private void dgvObjectives_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { if (dgvObjectives.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn) { dgvObjectives.EndEdit(); } } 
0
source

I had the same problem, and I tried many different ways to handle this, most of them failed, except when I tried this.BeginInvoke(new CDelegate()) .

-1
source

All Articles