以编程方式检查刚刚取消选中的 DataGridView CheckBox [英] Programmatically check a DataGridView CheckBox that was just unchecked

查看:22
本文介绍了以编程方式检查刚刚取消选中的 DataGridView CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人问过类似的问题,但没有一个解决方案对我有帮助.

I am aware that similar questions have been asked before, but none of the solutions are helping me.

我在未绑定的 DataGridView 中有一个 DataGridViewCheckBoxColumn.
CellContentClick 事件中,当 CheckBox 未选中时,我会根据 DataGridView 背后的业务规则提示用户是否要继续此操作,如果他们选择不继续,我想重新检查复选框.

I have a DataGridViewCheckBoxColumn in an unbound DataGridView.
In the CellContentClick event, when a CheckBox is unchecked, I am prompting the user whether they want to continue with this operation according to the business rules behind the DataGridView and, if they choose not to continue, I want to re-check the CheckBox.

正是这种重新检查 CheckBox 不起作用.

It is this re-checking of the CheckBox that is not working.

这是我的代码:

private void dgvPeriods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dgvPeriods.Columns["colSelected"].Index)
    {
        dgvPeriods.CommitEdit(DataGridViewDataErrorContexts.Commit);
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgvPeriods[e.ColumnIndex, e.RowIndex];

        if (chk.Value = chk.FalseValue)
        {
            If (MessageBox.Show("Continue with this Operation?", "Continue",  MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                chk.Value = chk.TrueValue;
                return;
            }
        }
    }
}

正在设置单元格的值,但视觉上未选中CheckBox.

The value of the cell is being set, but visually the CheckBox is not checked.

如果为 TrueValueFalseValue(布尔值与字符串)尝试了不同的类型,我已经尝试调用 Refresh(),我有尝试调用 CommitEdit(),我尝试使用 CheckState.Checked.

If have tried different types for the TrueValue and FalseValue (booleans vs strings), I have tried calling Refresh(), I have tried calling CommitEdit(), I have tried using CheckState.Checked.

我该怎么做才能直观地重新检查 CheckBox ?

What can I do to visually re-check the CheckBox ?

推荐答案

您可以在 CellContentClick 事件被引发,使用(正确的)EndEdit() 方法,因此 CellValueChanged1 事件也是立即引发而不是在当前 Cell 失去焦点之后.

You can commit the edit immediately after a CellContentClick event is raised, using the (proper) EndEdit() method, so the CellValueChanged1 event is also raised immediately instead of after the current Cell loses focus.

在这里评估新值:由于值已更改,因此当前值与前一个值相反,假设这是一个 bool 列.

Evaluate here the new Value: since the Value has changed, it's intended that the current value is the opposite of the previous, give that this is a bool Column.

此时,如果用户确认所做的选择,您将重置该值并调用 RefreshEdit() 以重绘当前状态的 CheckBox.

At this point, if the User confirms the choice made, you reset the value and call RefreshEdit() to redraw the CheckBox in its current state.

注意:您的 DataGridView 的行为可能取决于您的操作上下文.

Note: the behavior of your DataGridView may depend on the context of your operations.

private void dgvPeriods_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex != dgvPeriods.Columns["colSelected"].Index) return;

    bool newValue = (bool)dgvPeriods[e.ColumnIndex, e.RowIndex].Value;

    if (!newValue) {
        if (MessageBox.Show("Continue with this Operation?", "Continue", 
            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
            dgvPeriods[e.ColumnIndex, e.RowIndex].Value = true;
            dgvPeriods.RefreshEdit();
        }
    }
}

private void dgvPeriods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    // You need to evaluate whether EndEdit() applies to just this Column 
    if (e.ColumnIndex != dgvPeriods.Columns["colSelected"].Index) return;
    dgvPeriods.EndEdit();
}

1 - 请注意,在前一个事件处理程序中的代码完成之前真的立即引发此事件

1 - Note that this event is raised really immediately, before the code in the previous event handler completes

这篇关于以编程方式检查刚刚取消选中的 DataGridView CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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