不检查datagridview复选框列中最后检查的行

时间:2015-06-01 20:02:59

标签: c# datagridview datagridviewcheckboxcell

我从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为任意行。 如果我检查前两行。然后valid=true仅针对第一行。 如果我检查前3行。然后valid=true仅用于前两行。 似乎最后一行总是未经检查。但它被打了个。。这不仅仅是我从一开始就检查的。

我尝试了第二种方法,用于确定行是否被解除,结果是否相同。

(bool)dgvInputFile.Rows[currentRow].Cells["boolCol"].FormattedValue

另外。如果我检查第一个,那么第二个,第三个和 unceck第三个正常工作。

1 个答案:

答案 0 :(得分:2)

好的,我找到了导致这种行为的原因。 当我使用(bool)dgvInputFile.Rows[currentRow].Cells["boolCol"].FormattedValue

获取其值时,上次选中的复选框仍处于编辑模式

Insted我应该使用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.
相关问题