I have DataGridView populated from excel file. I added checkbox column to this datagridview
...
dtSet.Tables[0].Columns.Add(new DataColumn("boolCol", typeof(bool)));
dgvInputFile.DataSource = dtSet.Tables[0];
dgvInputFile.AllowUserToAddRows = false;
Later I'm checking if each row is cheked:
for (int currentRow = 0; currentRow < grv.Rows.Count; currentRow++)
{
DataGridViewCheckBoxCell CbxCell = dgvInputFile.Rows[currentRow].Cells["boolCol"] as DataGridViewCheckBoxCell;
bool valid = CbxCell != null && !DBNull.Value.Equals(CbxCell.Value) && (bool)CbxCell.Value == true;
....
If i check the first row. Then valid=false
for any row.
If i check the first 2 rows. Then valid=true
only for the first row.
If i check the first 3 rows. Then valid=true
only for the first two rows.
It seems like the last row is always unchecked. But it's cheked. And this is not only when i'm checking from the beginning.
I tried second method for determining if the row is cheked and the result is same.
(bool)dgvInputFile.Rows[currentRow].Cells["boolCol"].FormattedValue
In addition. If i check the first, then the second, and the third and unceck the third work fine.
OK, I found what cause this behavior.
Last checked checkbox is still in edit mode when i'm getting his value with (bool)dgvInputFile.Rows[currentRow].Cells["boolCol"].FormattedValue
Insted i should use DataGridViewCell.EditedFormattedValue
property.
MSDN
Gets the current, formatted value of the cell,
regardless of whether the cell is in edit mode
and the value has not been committed.