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