[英]Last checked row in datagridview checkbox column is not checked
我有从Excel文件填充的DataGridView。 我在此datagridview中添加了复选框列
...
dtSet.Tables[0].Columns.Add(new DataColumn("boolCol", typeof(bool)));
dgvInputFile.DataSource = dtSet.Tables[0];
dgvInputFile.AllowUserToAddRows = false;
稍后,我要检查每行是否被阻塞:
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;
....
如果我检查第一行。 然后,对于任何行, valid=false
。 如果我检查前2行。 然后valid=true
第一行valid=true
。 如果我检查前3行。 然后valid=true
前两行valid=true
。 似乎最后一行始终未选中。 但这很受宠。 这不仅是当我从头开始检查的时候。
我尝试了第二种方法来确定行是否被塞住,结果是否相同。
(bool)dgvInputFile.Rows[currentRow].Cells["boolCol"].FormattedValue
此外。 如果我检查第一个,那么第二个,第三个, 取消第三个工作正常。
OK,我找到了导致此行为的原因。 当我使用(bool)dgvInputFile.Rows[currentRow].Cells["boolCol"].FormattedValue
获取值时,最后选中的复选框仍处于编辑模式
我应该使用DataGridViewCell.EditedFormattedValue
属性。
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.