DataGridViewCheckBoxCell 如何在表单加载期间设置时显示已选中 [英] DataGridViewCheckBoxCell how to show checked when set during form load

查看:25
本文介绍了DataGridViewCheckBoxCell 如何在表单加载期间设置时显示已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 DataTable 加载数据的 DataGridView,以及一个未绑定的 DataGridViewCheckBoxCells 列.将 DataGridView 中的行与具有用户保存的值的单独 DataTable 进行比较,如果匹配,则应选中该行的复选框.

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.

这是比较值并将复选框值设置为true"的代码:

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

当我在表单加载期间调用方法来执行此操作时private void WQMDrill_Load(object sender, EventArgs e),值设置正确,但复选框未选中.但是,在表单加载完成后调用时,代码运行良好.不幸的是,我绝对需要在加载过程中检查这些.

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.

我希望我清楚我的问题,对此事的任何帮助将不胜感激.如您所见,我试图单独使单元格以及整个 DataGridView 控件无效.我也有

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.

推荐答案

您可以将复选框选择和更新逻辑放在 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.

这篇关于DataGridViewCheckBoxCell 如何在表单加载期间设置时显示已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆