2015-06-01 36 views
1

我有從Excel文件填充的DataGridView。 我添加複選框列到這個datagridview的DataGridview複選框列中的最後一次檢查行未被檢查

... 
dtSet.Tables[0].Columns.Add(new DataColumn("boolCol", typeof(bool))); 
dgvInputFile.DataSource = dtSet.Tables[0]; 
dgvInputFile.AllowUserToAddRows = false; 

後來我檢查如果各行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; 
    .... 

如果我檢查的第一行。然後valid=false爲任何行。 如果我檢查前2行。然後valid=true只適用於第一行。 如果我檢查前3行。然後valid=true只適用於前兩行。 似乎最後一行始終未被選中。但它是不好的。這不僅僅是當我從頭開始檢查的時候。

我嘗試了第二種方法來確定行是否被檢查並且結果是相同的。

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

另外,如果我檢查第一個,然後第二個,第三個和unceck第三個工作正常。

+0

做了'currentRow'實際上指向第3排(或)你打算要檢查的行? – Rahul

+0

是的。我檢查了這一點。只有複選框單元格的值不正確。 – cattaneo

回答

1

好的,我發現是什麼原因導致了這種行爲。 最後檢查複選框仍然處於編輯模式,當我得到他的價值(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.