DataGridView中的复选框不触发CellValueChanged事件

9

我正在使用如下代码:

// 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),就不能正常工作。

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

我需要做些什么来实现这个功能?


1
这是一个糟糕的标题,请更新。您可以阅读http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title。 - Soner Gönül
以下链接回答了你的问题:http://social.msdn.microsoft.com/Forums/windows/zh-CN/31c7a954-1a85-4c38-9e9f-e157d33faf0b/how-to-get-the-value-of-checkbox-in-the-datagridview - Colm McKenna
你的问题已经得到解答了吗?还是你仍然遇到了困难? - Derek W
4个回答

23

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

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

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的其他信息。


5

2
我想出了一个稍微不同的解决方案。
我使用CurrentCellDirtyStateChanged事件来检查列是否为复选框列,如果是,我手动触发CellValueChanged事件,如下所示:
if (DGV_Charge.CurrentCell.ColumnIndex == 0) DGV_Charge_CellValueChanged(null, new DataGridViewCellEventArgs(DGV_Charge.CurrentCell.ColumnIndex, DGV_Charge.CurrentCell.RowIndex));

0
最好的方法是通过创建自己的网格来扩展网格,并准备好这些各种“技巧”。相信我,在这个网格中需要进行很多调整。
建议使用以下代码:
Public Class MyGrid
    Inherits Windows.Forms.DataGridView

    Private Sub MyGrid_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
                       Handles Me.CurrentCellDirtyStateChanged
        If Me.IsCurrentCellDirty AndAlso Not Me.CurrentCell Is Nothing Then
           If TypeOf Me.Columns(Me.CurrentCell.ColumnIndex) Is DataGridViewCheckBoxColumn Then
              Me.CommitEdit(DataGridViewDataErrorContexts.Commit) 'imediate commit, not only on focus changed
           End If
        End If
    End Sub

    Private Sub MyGrid_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) 
                       Handles Me.CellValueChanged
        If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
           Dim Checked As Boolean = False
           If TypeOf Me.Columns(e.ColumnIndex) Is DataGridViewCheckBoxColumn Then
              'avoid erros
              Checked = Me.CellChecked(e.RowIndex, e.ColumnIndex)
           End If
           RaiseEvent Change(e.RowIndex, e.ColumnIndex, Checked)
        End If
    End Sub

End Class

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接