3

How can i check for the bool condition of a check box which is in datagridview. I would like to have true if checked and false if it was unchecked. Can any one help me.

Is it possible to handle this in dataGridView_CellContentClick

Developer
  • 8,390
  • 41
  • 129
  • 238

5 Answers5

7

This is addressed a little bit on the MSDN pages for the DataGridView here and here.

In particular they say:

For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView.CellValueChanged event instead. Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the DataGridView.CurrentCellDirtyStateChanged event. In that handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

So they recommend against using the CellClick type events (since they never push the value until you leave the cell) but instead use CurrentCellDirtyStateChanged and the CommitEdit method.

So you end up with:

dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CB")
    {
        MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());    
    }
}

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

And as for getting the checked value - this is just the Value property of the DataGridViewCheckBoxCell.

So if you go:

dataGridView1.Rows[rowindex].Cells[cellindex].Value 

you get a boolean value which corresponds to the checkbox (after the change has been committed).

David Hall
  • 32,624
  • 10
  • 90
  • 127
0

It works 100 %.

 private void grd_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            grd.CommitEdit(DataGridViewDataErrorContexts.Commit);
            bool Result = Convert.ToBoolean((grd[e.ColumnIndex, e.RowIndex] as DataGridViewCheckBoxCell).Value);
        }
garish
  • 637
  • 12
  • 14
0

if the checkbox is defined in the designer it would be as simple asreferring to the checkbox´s name and checking its "checked" property for true/false.

But i suspect you are adding the checkbox to the datagrid by code?

in this case youll have to save a reference to the checkbox somwhere. If i where you i would add all the checkboxes that i add to the datagrid to a list or if you want to refer to them by name i would add them to a dictionary.

You could also bind an event to the checkbox Checked_Changed event by selecting it and clicking the little bolt icon in the properties panel and finding the checkedChanged-event and doubleclicking it.

in the event-code you can obtain the checkbox clicked by typing: CheckBox mycheckbox = sender as CheckBox;

and then refering to mycheckbox.checked to get a bool for if its checked or not.

  • I am unable to get the `checked` property when i refered – Developer Oct 28 '11 at 11:34
  • Sorry, C# is case-sensitive so youll have to refer to "Checked" (With an uppercase C) my bad... – Mats Edvinsson Oct 28 '11 at 11:36
  • That's what i am saying man if we find using DataGridViewCheckBoxCell we can get only `value` not `Checked` – Developer Oct 28 '11 at 11:40
  • Ok, now that you mentioned DataGridViewCheckBoxCell i understand what you mean. i think that when you set up the column you will have to set values in the FalseValue and TrueValue. then when you refer to the checkbox.Value you get what you entered in FalseValue if its unchecked, otherwise you get TrueValue. Hope that helps :) – Mats Edvinsson Oct 28 '11 at 11:53
  • theres no code for acheiving what i said. You said you can get DataGridViewCheckBoxCell.Value right? when you are in the designer, right-click the datagridview and click edit columns. select your checkbox-column and look for the "Data"-tab in the right pane and enter a value in FalseValue that you want to get from DataGridViewCheckBoxCell.Value when the checkbox is unchecked, and then enter a value in TrueValue that you want to get when it is checked – Mats Edvinsson Oct 28 '11 at 12:05
  • and if you are adding the checkbox by code you can do like this: ` private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { var checkcell = new DataGridViewCheckBoxCell(); checkcell.FalseValue = false; checkcell.TrueValue = true; dataGridView1[0, 0] = checkcell; //Adding the checkbox if (((bool)((DataGridViewCheckBoxCell)dataGridView1[0, 0]).Value) == true) { //Stuff to do if the checkbox is checked } } ` – Mats Edvinsson Oct 28 '11 at 12:29
  • ill post that as a new answer because that is hardly readable. – Mats Edvinsson Oct 28 '11 at 12:31
0

You can try to get this in this fashion, say in case you are looping the grid the based on the index you can find the checked state.

bool IsChecked = Convert.ToBoolean((dataGridView1[ColumnIndex, RowIndex] as DataGridViewCheckBoxCell).FormattedValue))
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • But as per my requirement this will always be `false` as i am checking the condition in `dataGridView_CellContentClick` – Developer Oct 28 '11 at 12:08
0
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    var checkcell = new DataGridViewCheckBoxCell();
    checkcell.FalseValue = false;
    checkcell.TrueValue = true;
    checkcell.Value = false;
    dataGridView1[0, 0] = checkcell; //Adding the checkbox

    if (((bool)((DataGridViewCheckBoxCell)dataGridView1[0, 0]).Value) == true)
    {
        //Stuff to do if the checkbox is checked
    }
}
  • But this will not work when i click on the check box for first time when it was in unchecked state. This will work when check box is in checked state – Developer Oct 28 '11 at 12:40
  • ahh, noticed that. but with my edit it works. just add: checkcell.Value = false; – Mats Edvinsson Oct 28 '11 at 13:17