[英]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.