How to verify if a DataGridViewCheckBoxCell is Checked
c#, datagridview, datagridviewcheckboxcell, winforms
Solution
Thank you all. Had the same problem but i find out that writing senderGrid.EndEdit(), before checking the value, resolves it.
private void dgvRiscos_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
senderGrid.EndEdit();
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
e.RowIndex >= 0)
{
var cbxCell = (DataGridViewCheckBoxCell)senderGrid.Rows[e.RowIndex].Cells["associado"];
if ((bool)cbxCell.Value)
{
// Criar registo na base de dados
}
else
{
// Remover registo da base de dados
}
}
}
Keep up the good work
Problem
I have bound a data table to a `DataGridView`, this data table has a column called "Status" which is of type `Boolean`. I can set the value to `true` or `false` just fine via code. However, I can't figure out how to check to see if the given row is already checked or not. This is the code I am trying to use and compiling it shows the error "the specified cast is invalid". Any help would be appreciated. ``` if (rowIndex >= 0) { var cbxCell = (DataGridViewCheckBoxCell)dgvScan.Rows[rowIndex].Cells["Status"]; if ((bool)cbxCell.Value) { // Do stuff } else { // Do other stuff } } ```