C# 未选中datagridview复选框列中最后选中的行

C# 未选中datagridview复选框列中最后选中的行,c#,datagridview,datagridviewcheckboxcell,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 =

我已经从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; 
    ....

另外。如果我先检查第一个,然后检查第二个,再检查第三个,然后取消检查第三个,工作正常。

好的,我找到了导致这种行为的原因。 当我使用
(bool)dgvInputFile.Rows[currentRow].Cells[“boolCol”].FormattedValue获取其值时,上次选中的复选框仍处于编辑模式

相反,我应该使用
DataGridViewCell.EditedFormattedValue
属性

MSDN


currentRow
是否实际指向第三行(或)您要检查的行?是。我查过了。只有复选框单元格的值不正确。
(bool)dgvInputFile.Rows[currentRow].Cells["boolCol"].FormattedValue
Gets the current, formatted value of the cell, 
regardless of whether the cell is in edit mode 
and the value has not been committed.