DataGridViewCheckBoxCell 如何在表单加载期间设置时显示选中状态
我有一个从 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);
}
}
,这段时间不会触发。谢谢。
I have a DataGridView that loads data from a DataTable, along with an unbound column of DataGridViewCheckBoxCells. The rows in the DataGridView are compared with a separate DataTable with values the user has saved, and if there is a match, the checkbox for that row should check.
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 during the form load private void WQMDrill_Load(object sender, EventArgs e)
, the values are set correctly, but the checkboxes do not check. However, when called after the form is finished loading, the code works perfectly. Unfortunately for me, I absolutely need for these to check during the load process.
I hope I was clear with my issue, any help on this matter would be greatly appreciated. As you can see, I have tried to invalidate the cell alone, 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);
}
}
That doesn't fire during this time. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将复选框选择和更新逻辑放入 DataBindingComplete 事件处理程序中,该事件处理程序在 FormLoad 之后但在向用户显示任何内容之前触发。
You can put your checkbox selection and update logic in the DataBindingComplete eventhandler, this fires after the FormLoad but before anything is displayed to the user.
我不确定调用
CommitEdit
是否会真正触发单元格上的Paint
。如果该列是复选框列,请尝试处理CellMouseUp
事件并触发EndEdit
。I'm not certain that calling
CommitEdit
will actually fire thePaint
on the cell. Try handling theCellMouseUp
event and firingEndEdit
if the column is a checkbox column.我遇到了同样的问题,并尝试了很多不同的方法来处理它,大多数都失败了,除了当我尝试
this.BeginInvoke(new CDelegate())
时。I had the same problem, and tried a lot of different ways to deal with it, most failed, except when I tried
this.BeginInvoke(new CDelegate())
.