Search code examples
c#winformsdatagridviewdatagridviewcheckboxcell

Check/Uncheck a checkbox on datagridview


Can someone help me why it doesn't work? I have a checkbox and if I click on it, this should uncheck all the checkbox inside the datagridview which were checked before including the user selected checkbox.

Here is the code:

        private void chkItems_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in datagridview1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
                if (chk.Selected == true)
                {
                    chk.Selected = false;
                }
                else
                {
                    chk.Selected = true;
                }
            }
        }

the checkbox should not be selected. it should be checked.

here is the added column

            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckBox chk = new CheckBox();
            CheckboxColumn.Width = 20;
            datagridview1.Columns.Add(CheckboxColumn);

Solution

  • Looking at this MSDN Forum Posting it suggests comparing the Cell's value with Cell.TrueValue.

    So going by its example your code should looks something like this:(this is completely untested)

    Edit: it seems that the Default for Cell.TrueValue for an Unbound DataGridViewCheckBox is null you will need to set it in the Column definition.

    private void chkItems_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
            if (chk.Value  == chk.TrueValue)
            {
                chk.Value = chk.FalseValue;
            }
            else
            {
                chk.Value = chk.TrueValue;
            }
        }
    }
    

    This code is working note setting the TrueValue and FalseValue in the Constructor plus also checking for null:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckboxColumn.TrueValue = true;
            CheckboxColumn.FalseValue = false;
            CheckboxColumn.Width = 100;
            dataGridView1.Columns.Add(CheckboxColumn);
            dataGridView1.Rows.Add(4);
        }
    
        private void chkItems_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                if (chk.Value == chk.FalseValue || chk.Value == null)
                {
                    chk.Value = chk.TrueValue;
                }
                else
                {
                    chk.Value = chk.FalseValue;
                }
    
            }
            dataGridView1.EndEdit();
        }
    }