DataGridView中的复选框未触发CellValueChanged事件 [英] Checkboxes in DataGridView not firing CellValueChanged event

查看:124
本文介绍了DataGridView中的复选框未触发CellValueChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

// Cell value change event.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if ((bool)dataGridView1.CurrentCell.Value == true) MessageBox.Show("true");
    if ((bool)dataGridView1.CurrentCell.Value == false) MessageBox.Show("false");

    MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}

除所有带有复选框的列( DataGridViewCheckBoxColumn

It works fine for all columns, except for one column with a checkbox (DataGridViewCheckBoxColumn)

我需要知道复选框列中的值(对或错)。

I need to know the value in the checkbox column (true or false).

我需要做什么?

推荐答案

使用 DataGridViewCheckBoxColumn 有时可能会有些棘手,因为有些规则专门仅适用于此列类型的 Cells 。此代码应处理您遇到的问题。

Working with DataGridViewCheckBoxColumn can sometimes be a bit tricky since there are some rules that specifically apply only to the Cells of this column type. This code should handle the issue that you are experiencing.

CurrentCellDirtyStateChanged 事件在单击单元格后立即提交更改。您在调用 CommitEdit 方法时手动引发 CellValueChanged 事件。

The CurrentCellDirtyStateChanged event commits the changes immediately when the cell is clicked. You manually raise the CellValueChanged event when calling the CommitEdit method.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentCell == null) return;
    if ((bool)dataGridView1.CurrentCell.Value == true) MessageBox.Show("true");
    if ((bool)dataGridView1.CurrentCell.Value == false) MessageBox.Show("false");
    MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

访问此处以了解有关使用 DataGridViewCheckBoxCell

这篇关于DataGridView中的复选框未触发CellValueChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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