How to detect DataGridView CheckBox event change?

后端 未结 16 2382
离开以前
离开以前 2020-11-27 12:43

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either

相关标签:
16条回答
  • 2020-11-27 13:40

    Removing the focus after the cell value changes allow the values to update in the DataGridView. Remove the focus by setting the CurrentCell to null.

    private void DataGridView1OnCellValueChanged(object sender, DataGridViewCellEventArgs dataGridViewCellEventArgs)
    {
        // Remove focus
        dataGridView1.CurrentCell = null;
        // Put in updates
        Update();
    }
    
    private void DataGridView1OnCurrentCellDirtyStateChanged(object sender, EventArgs eventArgs)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 13:44

    What worked for me was CurrentCellDirtyStateChanged in combination with datagridView1.EndEdit()

    private void dataGridView1_CurrentCellDirtyStateChanged( object sender, EventArgs e ) {
        if ( dataGridView1.CurrentCell is DataGridViewCheckBoxCell ) {
            DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)dataGridView1.CurrentCell;
            if ( (byte)cb.Value == 1 ) {
                dataGridView1.CurrentRow.Cells["time_loadedCol"].Value = DateTime.Now.ToString();
            }
        }
        dataGridView1.EndEdit();
    }
    
    0 讨论(0)
  • 2020-11-27 13:44

    I've tried some answers from here, but I've always had some kind of problem (like double clicking or using the keyboard). So, I combined some of them and got a consistent behavior (it's not perfect, but works properly).

    void gridView_CellContentClick(object sender, DataGridViewCellEventArgs e) {
      if(gridView.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell))
        return;
      if(!gridView.CurrentCell.IsInEditMode)
        return;
      if(!gridView.IsCurrentCellDirty)
        return;
      gridView.EndEdit();
    }
    
    void gridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {
      if(e.ColumnIndex == gridView.Columns["cFlag"].Index && e.RowIndex >= 0)
        gridView.EndEdit();
    }
    
    void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
      if(e.ColumnIndex != gridView.Columns["cFlag"].Index || e.RowIndex < 0)
        return;
    
      // Do your stuff here.
    
    }
    
    0 讨论(0)
  • 2020-11-27 13:45

    following Killercam'answer, My code

    private void dgvProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvProducts.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    

    and :

    private void dgvProducts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvProducts.DataSource != null)
            {
                if (dgvProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
                {
                    //do something
                }
                else
                {
                   //do something
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题