【问题标题】:Checkboxes in DataGridView not firing CellValueChanged eventDataGridView 中的复选框未触发 CellValueChanged 事件
【发布时间】:2013-06-20 22:35:54
【问题描述】:

我正在使用此代码:

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

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

为此我需要做什么?

【问题讨论】:

标签: c# .net winforms checkbox datagridview


【解决方案1】:

使用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);
    }
}

访问here 了解有关使用DataGridViewCheckBoxCell 的更多信息。

【讨论】:

    【解决方案2】:

    MSDN 说 here 在单元格失去焦点之前不会触发 CellValueChanged。

    一些解决方案:

    DataGridView.CellContentClick

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        最好的办法是通过创建自己的网格来扩展网格,并准备好使用这些不同的“技巧”。相信我,这个网格中有很多东西需要调整。

        建议使用的代码

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-30
          • 1970-01-01
          • 1970-01-01
          • 2015-08-09
          • 2011-11-17
          • 2016-04-13
          • 1970-01-01
          相关资源
          最近更新 更多