Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView中的复选框未触发CellValueChanged事件_C#_.net_Winforms_Checkbox_Datagridview - Fatal编程技术网

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

C# DataGridView中的复选框未触发CellValueChanged事件,c#,.net,winforms,checkbox,datagridview,C#,.net,Winforms,Checkbox,Datagridview,我正在使用以下代码: // 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.V

我正在使用以下代码:

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

我需要为此做些什么?

MSDN说,CellValueChanged在单元格失去焦点之前不会启动

一些解决方案:


使用
DataGridViewCheckBoxColumn
有时会有点棘手,因为有些规则只适用于此列类型的
单元格。此代码应处理您遇到的问题

单击单元格时,
CurrentCellDirtyStateChanged
事件立即提交更改。调用
committedit
方法时,手动引发
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

的更多信息,请访问以获取更多信息。最好是通过创建自己的网格来扩展网格,并准备好这些“技巧”。相信我,在这个网格中有很多东西需要调整

建议使用的代码

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

我想出了一个稍微不同的解决方案

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

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

这是一个糟糕的标题。请更新它。你可以阅读下面的链接来回答你的问题:你的问题得到回答了吗?还是你仍然有问题?