Check All Checkbox items on DataGridView

63,035

Solution 1

DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];

instead of

DataGridViewCheckBoxCell chk = e.row.Cell(0);

*EDIT:*I think you really want to do this:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
       DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
       chk.Value = !(chk.Value == null ? false : (bool) chk.Value); //because chk.Value is initialy null
}

Solution 2

    private void setCheckBoxInDataGrid(DataGridView dgv, int pos, bool isChecked)
    {
        for (int i = 0; i < dgv.RowCount; i++)
        {
            dgv.Rows[i].DataGridView[pos, i].Value = isChecked;
        }
    }

This is how I did it

Solution 3

Try this one

foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
      row.Cells[0].Value = row.Cells[0].Value == false ? true : false;

}
Share:
63,035
user1647667
Author by

user1647667

Updated on July 09, 2022

Comments

  • user1647667
    user1647667 almost 2 years

    Here's the scenario.

    I have checkbox(Name:"Check All" ID:chkItems) and datagridview. And when I click on this checkbox, all checkboxes on the datagridview will also be checked.

    I've also added the checkbox column on the grid.

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

    Here is the code behind of the checkbox. There is a problem on the row.Cell

    private void chkItems_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in GridView1.Rows)
        {
            DataGridViewCheckBoxCell chk = e.row.Cells(0);
            if (chk.Selected == false)
            {
                row.Cells(0).Value = true;
            }
        }
    }   
    
  • user1647667
    user1647667 over 11 years
    when i tried this one, DataGridViewCheckBoxCell chk = row.Cells[0]; there is still error on row.cells[0];
  • Nikola Davidovic
    Nikola Davidovic over 11 years
    check the edit, you need to cast the Cell to DataGridViewCheckBoxCell
  • user1647667
    user1647667 over 11 years
    there's error on the checkbox column that i created // Unable to cast object of type 'System.Windows.Forms.DataGridViewImageCell' to type 'System.Windows.Forms.DataGridViewCheckBoxCell'.
  • Nikola Davidovic
    Nikola Davidovic over 11 years
    What is the index of the DataGridViewCheckBoxColumn in your datagridview?
  • user1647667
    user1647667 over 11 years
    i've added this column DataGridViewCheckBoxColumn CheckboxColumn = new DataGridView CheckBoxColumn(); CheckBox chk = new CheckBox(); CheckboxColumn.Width = 20; GridView1.Columns.Add(CheckboxColumn);
  • user1647667
    user1647667 over 11 years
    Thanks! I'm done with it. but there's a problem. there's no chk.Checked it chk.Selected. that's why it's only selecting the item. how can i check the checkbox?
  • Nikola Davidovic
    Nikola Davidovic over 11 years
    I see it from the code you posted but there is also some other column in you dataGridView that is making problems
  • Nikola Davidovic
    Nikola Davidovic over 11 years
    I've posted that edit already, load it. You should access chk.Value
  • smulholland2
    smulholland2 over 9 years
    I used this answer but another one was a little more helpful in case you need to select all checkboxes after some user input that might set a checkbox cell into edit mode. In that case, this answer leaves the edit mode cells as they are. Replace chk.Value !=... in the answer about with this:chk.Value = !(chk.EditedFormattedValue.ToString() == "false" ? false : (bool)chk.EditedFormattedValue); and you should be in good shape.
  • smulholland2
    smulholland2 over 9 years
    It also helps to set the dgv.DefaultCell = null;