![](/img/trans.png)
[英]Error: Index was out of range. Must be non-negative and less than the size of the collection
[英]Index was out of range. Must be non-negative and less than the size of the collection error
嗨,我在運行這段代碼時收到此錯誤,無法弄清楚。 干杯:
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
for (int i = 0; i <= (DataGridView1.Rows.Count); i++)
{
if (DataGridView1.Rows[i].Cells[2].ToString().Equals(returnID))
{
SignIn_Time(updateCmd, OLEDB_Connection, varName, varID, varTime);
break;
}
}
}
catch
{
由於從零開始的索引,它應該小於計數,而不是等於:
i < DataGridView1.Rows.Count
該錯誤是明確的,這是怎么回事:
[索引]非負且小於集合的大小。
替換<=
與<
在循環頭:
for (int i = 0; i < (DataGridView1.Rows.Count); i++) {
...
}
此外,您需要確保每行至少包含三個單元格,因為您要訪問一行中的第三個單元格: Cells[2]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.