Question

How can I programmatically uncheck all rows in a DataGridViewCheckboxColumn in a datagridview?

I can get the correct value of the checkbox using

(bool)row.Cells[CheckBoxColumn.Index].FormattedValue

but that's only a getter.

I have tried setting the value of the cell using

(bool)row.Cells[CheckBoxColumn.Index].value = false

but that doesn't affect the FormattedValue.

How can I solve this?

Was it helpful?

Solution

You do sth. like:

(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;

You just forgot to cast to the correct type, a generic DataGridViewCell doesn't know its value-type.

OTHER TIPS

Have you tried casting the first control in the checkbox column to checkbox and then setting 'Checked' to true?

Try something to this extent.

((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = true

Haven't checked but you can try;

CheckBox cb = (row.Cells[CheckBoxColumn.Index].Controls[0] as CheckBox);
if(cb != null)
{
    cb.Checked = false;
}

It's type may be different. Just debug and cast it to what it is.

foreach (DataGridViewRow dr in dataGridView1.Rows)
{
  dr.Cells[0].Value = true;//sıfırın
}

If you use dataGridView1_ContextClick just for do "false" datagidviewCheckBox Column need this Code :

dataGridView1.CancelEdit();

but if you need all rows of CheckBoxColumns of DataGrid :

private void button1_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow r in dataGridView1.Rows)
    {
        r.Cells["statusBox"].Value = true;
    }
}

Depending on what you wish to do

If it is about the selected row, then you can:

DataGridViewRow row = dataGridViewName.CurrentRow;

//This will assign the opposite value of the Cell Content
row.Cells["ColumnName"].Value = !Convert.ToBoolean(row.Cells["ColumnName"].Value);

However if you wish to do it for the whole DataGridView table then:

    foreach (DataGridViewRow row in dataGridViewName.Rows)

   {
        //This will assign the opposite value of the Cell Content
        row.Cells["ColumnName"].Value = !Convert.ToBoolean(row.Cells["ColumnName"].Value);
   }

Whatever suites you. Keep it up!

Loop through each row of grid view and use the find control method:

foreach ( GridViewRow row in myGridView )
{
     CheckBox checkBox = ( CheckBox ) row.FindControl( "myCheckBox" );
     checkbox.Checked = false;
}
 foreach (DataGridViewRow row in datagridviewname.Rows)

  {
    row.Cells[CheckBoxColumn1_Name].Value = false;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top