How to detect DataGridView CheckBox event change?

后端 未结 16 2380
离开以前
离开以前 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:21

    jsturtevants's solution worked great. However, I opted to do the processing in the EndEdit event. I prefer this approach (in my application) because, unlike the CellValueChanged event, the EndEdit event does not fire while you are populating the grid.

    Here is my code (part of which is stolen from jsturtevant:

    private void gridCategories_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == gridCategories.Columns["AddCategory"].Index)
        {
            //do some stuff
        }
    }
    
    
    
    private void gridCategories_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.ColumnIndex == gridCategories.Columns["AddCategory"].Index)
        {
            gridCategories.EndEdit();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:22

    You can force the cell to commit the value as soon as you click the checkbox and then catch the CellValueChanged event. The CurrentCellDirtyStateChanged fires as soon as you click the checkbox.

    The following code works for me:

    private void grid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            SendKeys.Send("{tab}");
        }
    

    You can then insert your code in the CellValueChanged event.

    0 讨论(0)
  • 2020-11-27 13:23

    Here is some code:

    private void dgvStandingOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (dgvStandingOrder.Columns[e.ColumnIndex].Name == "IsSelected" && dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
        {
            bool isChecked = (bool)dgvStandingOrder[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
            if (isChecked == false)
            {
                dgvStandingOrder.Rows[e.RowIndex].Cells["Status"].Value = "";
            }
            dgvStandingOrder.EndEdit();
        }
    }
    
    private void dgvStandingOrder_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
    
        dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    
    private void dgvStandingOrder_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
        {
            dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:24

    To handle the DatGridViews CheckedChanged event you must first get the CellContentClick to fire (which does not have the CheckBoxes current state!) then call CommitEdit. This will in turn fire the CellValueChanged event which you can use to do your work. This is an oversight by Microsoft. Do some thing like the following...

    private void dataGridViewSites_CellContentClick(object sender, 
        DataGridViewCellEventArgs e)
    {
        dataGridViewSites.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    
    /// <summary>
    /// Works with the above.
    /// </summary>
    private void dataGridViewSites_CellValueChanged(object sender, 
        DataGridViewCellEventArgs e)
    {
        UpdateDataGridViewSite();
    }
    

    I hope this helps.

    P.S. Check this article https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx

    0 讨论(0)
  • 2020-11-27 13:26

    This also handles the keyboard activation.

        private void dgvApps_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dgvApps.CurrentCell.GetType() == typeof(DataGridViewCheckBoxCell))
            {
                if (dgvApps.CurrentCell.IsInEditMode)
                {
                    if (dgvApps.IsCurrentCellDirty)
                    {
                        dgvApps.EndEdit();
                    }
                }
            }
        }
    
    
        private void dgvApps_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
              // handle value changed.....
        }
    
    0 讨论(0)
  • 2020-11-27 13:27

    The Code will loop in DataGridView and Will check if CheckBox Column is Checked

    private void dgv1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.ColumnIndex == 0 && e.RowIndex > -1)
        {
            dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            var i = 0;
            foreach (DataGridViewRow row in dgv1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                {
                    i++;
                }
            }
    
            //Enable Button1 if Checkbox is Checked
            if (i > 0)
            {
                Button1.Enabled = true;
            }
            else
            {
                Button1.Enabled = false;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题