Datagridview checkbox checked when clicking the cell
Asked Answered
C

5

7

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;
    }

}
Canoe answered 1/4, 2015 at 19:13 Comment(4)
is this XAML? that would be a better tag for the question than [cell] and [checked]Johnsten
Why would this question get a minus vote?Canoe
The downvote is because there are a lot of opinionated idiots at SO who think that's the best way to add value.Kendallkendell
I agree, these idiots should rather give a better solution, or, at least a valid reason along with a up/down vote instead of being biased.Canoe
A
15

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();
}
Algerian answered 2/4, 2015 at 17:10 Comment(5)
Thanks for the replies. I get the 'specified cast is not valid' exception: (bool)cell.valueCanoe
@Canoe What is the value of the cell if not True or False?Algerian
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!Canoe
Glad to help! Good job figuring out that fix.Algerian
Works great. You can get rid of the "cell.Value == null ||" since its guaranteed to not be null at that line.Shroyer
S
1

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);
    }
}
Santa answered 2/4, 2015 at 7:6 Comment(0)
A
1
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!

Aureaaureate answered 15/11, 2016 at 6:55 Comment(0)
S
0

A more generic approach to OhBeWise solution with a static function in HelperClass

public static void DataGrid_CheckBoxCellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dataGrid = sender as DataGridView;
    DataGridViewCheckBoxCell cell = dataGrid?.CurrentCell as DataGridViewCheckBoxCell;

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

In your code behind

dataGridView1.CellClick += HelperClass.DataGrid_CheckBoxCellClick;

Sanford answered 8/9, 2022 at 6:42 Comment(0)
A
0

Code bellow is not necessary :-)

if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)

You can write it simplier:

if(!dataGridView.Columns[e.ColumnIndex].ReadOnly)
Acquit answered 29/9, 2022 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.