1

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.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
MinhKiyo
  • 191
  • 3
  • 15
  • _"not able to do it"_ ....what didn't work? – Raktim Biswas Oct 02 '16 at 16:26
  • I apologize for not clearly explaining. "not able to do it" .... means: the inside datagridview, if I uncheck a checkbox, all checkbox is unchecked. However, thanks to the guidance of Mr.TaW as below, I did it run well. – MinhKiyo Oct 02 '16 at 21:51

2 Answers2

1

DataGridViewCheckBoxCell has properties TrueValue, FalseValue and IndeterminateValue whose default values are null. These are the values given by the property Value depending on the state of the checkbox. As Convert.ToBoolean converts null to false, the result of the conversion is always false if the properties are not initialized.

These can be initialized on the cell itself or on its owning column.

You need thus to initialize the owning column TrueValue to true and FalseValue to false.

Philippe
  • 1,225
  • 7
  • 9
1

Setting the TrueValue, FalseValue and IndeterminateValue is a good start.

I found that I also need to do a bit more for this to work; in addition to your CurrentCellDirtyStateChanged event I also coded these:

This sets all CheckBoxCells:

private void cbx_all_CheckedChanged(object sender, EventArgs e)
{
    if (cbx_all.Tag == null) for (int i = 0; i < dataGridView.RowCount; i++)
    {
        dataGridView.Tag = "busy";
        dataGridView[yourCheckBoxcolumnIndex, i].Value = cbx_all.Checked;
        dataGridView.Tag = null;
    }
}

I coded the CellValueChanged instead of the CellContentClick event:

private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == yourCheckBoxcolumnIndex &&  dataGridView.Tag == null)
    {
       cbx_all.Tag = "busy";
       cbx_all.Checked = testChecks(e.ColumnIndex);
       cbx_all.Tag = null;
    }
}

I used the Tag property of the DGV and the CheckBox as a flag, that I am busy changing values by code. Some other means to avoid an endless-loop are just as fine.

This is the test function:

bool testChecks(int index)
{
    for (int r = 0; r < dataGridView.RowCount; r++)
        if  ( !(bool)dataGridView[index, r].Value ) return false;
    return true;
}
TaW
  • 53,122
  • 8
  • 69
  • 111