繁体   English   中英

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

[英]Checkboxes in DataGridView not firing CellValueChanged event

我正在使用此代码:

// 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)。

我需要做什么呢?

使用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其他信息,请访问此处

MSDN 在这里说CellValueChanged在小区失去焦点之前不会触发。

一些解决方案

DataGridView.CellContentClick

http://codingeverything.blogspot.com/2013/01/firing-datagridview-cellvaluechanged.html

我提出了一个略有不同的解决方案。

我使用CurrentCellDirtyStateChanged事件检查列是否是复选框列,如果是,我手动触发CellValueChanged事件,如下所示:

if (DGV_Charge.CurrentCell.ColumnIndex == 0) DGV_Charge_CellValueChanged(null, new DataGridViewCellEventArgs(DGV_Charge.CurrentCell.ColumnIndex, DGV_Charge.CurrentCell.RowIndex));

最好的方法是通过创建自己的网格来扩展网格,准备好这些各种“技巧”。 相信我,在这个网格中需要调整很多东西。

建议的代码使用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM