好吧,我要說,我不能再現你遇到的問題。但是,您提到錯誤實際發生在dataGridView1.CurrentCell = cell;
,應該排除-1索引錯誤。
此外,你說你得到的錯誤是Index -1 does not have a value.
這意味着,即使你有正確的索引,cell
仍然是索引-1。這意味着單元格不存在或者其他粗略的事情正在發生。既然你聽起來像你已經有一段時間了,我認爲這個單元格實際上存在。
由於錯誤似乎沒有在您發佈的4行中的任何一行中,我會說看看別的地方,比如當您第一次將源綁定到datagridview時。
更新:我只是找到了幾個與此相關的鏈接。由於我不知道你是如何綁定datagridview的,所以我不知道它們是否適用,但如果有的話,請告訴我們!無論如何,它似乎可能適用於綁定。
來源:SO Question 1
If you initially bind an empty collection that does not inform the DGV of changes (e.g. a Collection does not, but a BindingList does), the initial current row offset will be correctly set to -1, (Because it is empty.)
When you subsequently add objects to your data bound collection they will still display correctly on the grid, but the CurrencyManager will not be informed of any changes, and the current row offset will remain stubbornly at -1.
So, when you try to edit a row, the CurrencyManager thinks you are trying to edit a row at offset -1, and the exception is thrown.
To combat this, you need to rebind before interacting with a row, or initially bind a Collection etc when it contains one or more items.
SO Question 2
.NET Monster Question
此代碼運行時有什麼行數?是0嗎? – David 2011-04-03 21:02:10
RowCount = 1我在'dataGridView1.CurrentCell = cell; 'line – ErocM 2011-04-03 21:08:23