我有一个从 DataTable 加载数据的 DataGridView,以及一个未绑定的 DataGridViewCheckBoxCells 列。DataGridView 中的行将与具有用户保存的值的单独 DataTable 进行比较,如果匹配,则应选中该行的复选框。
这是比较值并将复选框值设置为“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;
}
}
}
当我在表单加载期间调用该方法执行此操作时private void WQMDrill_Load(object sender, EventArgs e)
,值设置正确,但复选框未选中。但是,在表单加载完成后调用时,代码可以完美运行。对我来说不幸的是,我绝对需要在加载过程中检查这些。
我希望我对我的问题很清楚,对此问题的任何帮助将不胜感激。如您所见,我试图单独使单元格以及整个 DataGridView 控件无效。我也有
private void dgvObjectives_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dgvObjectives.CurrentCell.ColumnIndex == 0)
{
this.dgvObjectives.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
在此期间不会触发。谢谢你。