Datagridview checkbox checked when clicking the cell

15,057

Solution 1

As Bioukh points out, you must call NotifyCurrentCellDirty(true) to trigger your event handler. However, adding that line will no longer update your checked state. To finalize your checked state change on click we'll add a call to RefreshEdit. This will work to toggle your cell checked state when the cell is clicked, but it will also make the first click of the actual checkbox a bit buggy. So we add the CellContentClick event handler as shown below and you should be good to go.


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
  DataGridViewCheckBoxCell cell = this.dataGridView1.CurrentCell as DataGridViewCheckBoxCell;

  if (cell != null && !cell.ReadOnly)
  {
    cell.Value = cell.Value == null || !((bool)cell.Value);
    this.dataGridView1.RefreshEdit();
    this.dataGridView1.NotifyCurrentCellDirty(true);
  }
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
  this.dataGridView1.RefreshEdit();
}

Solution 2

This should do what you want :

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)
    {       
        dataGridView.CurrentCell.Value = true;
        dataGridView.NotifyCurrentCellDirty(true);
    }
}

Solution 3

private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView3.Columns["Select"].Index)//checking for select click
        {
            dataGridView3.CurrentCell.Value = dataGridView3.CurrentCell.FormattedValue.ToString() == "True" ? false : true;
            dataGridView3.RefreshEdit();
        }
    }

These Changes had Worked for me!

Share:
15,057
Jnr
Author by

Jnr

Developer

Updated on June 10, 2022

Comments

  • Jnr
    Jnr almost 2 years

    I handle my checkbox click event with the CurrentCellDirtyStateChanged. What I want to be able to do is handle the same event when I click the cell that contains the checkbox too, i.e. when I click the cell, check the checkbox and call the DirtyStateChanged. Using the following code does not help much, it does not even call the CurrentCellDirtyStateChanged. I've run out of ideas.

    private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)
        {       
              //option 1
              (dataGridView.CurrentRow.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell).Value = true;
              //option 2
              DataGridViewCheckBoxCell cbc = (dataGridView.CurrentRow.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell);
              cbc.Value = true;
              //option 3
              dataGridView.CurrentCell.Value = true;
        }
    
    }
    
  • Jnr
    Jnr about 9 years
    Thanks for the replies. I get the 'specified cast is not valid' exception: (bool)cell.value
  • OhBeWise
    OhBeWise about 9 years
    @Jnr What is the value of the cell if not True or False?
  • Jnr
    Jnr about 9 years
    That's a good question and I did not mention that the datagridview is data bound. So the value is DBNull. Instead of using the cast to bool, I used dataGridView.CurrentCell.Value = true; Thanks for the help!
  • OhBeWise
    OhBeWise about 9 years
    Glad to help! Good job figuring out that fix.
  • FoppyOmega
    FoppyOmega about 8 years
    Works great. You can get rid of the "cell.Value == null ||" since its guaranteed to not be null at that line.