dimanche 2 octobre 2016

How to handle event of combobox in datagridview?

I have a checkbox[All] and a datagridview as below:

enter image description here

I would like:

  • The inside datagridview, if all checkbox is checked, the checkbox[All] is checked and otherwise if all checkbox is unchecked, the checkbox[All] is unchecked
  • The inside datagridview, there is a checkbox that is unchecked, the checkbox[All] is unchecked

I tried it but I am not able to do it:

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    bool isAllCheck = false;

    if (e.ColumnIndex == 0)
    {
        foreach (DataGridViewRow row in dataGridView.Rows)
        {
            DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;

            isAllCheck = Convert.ToBoolean(chk.Value);
            if (!isAllCheck)
            {
                break;
            }
        }

        if (chkAllItem.Checked && !isAllCheck)
        {
            chkAllItem.Checked = false;
        }

        if (!chkAllItem.Checked && isAllCheck)
        {
            chkAllItem.Checked = true;
        }
    }
}

private void dataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (this.dataGridView.IsCurrentCellDirty)
    {
        this.dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

Any tips on these will be great help. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire