[英]Index was out of range. Must be non-negative and less than the size of the collection error
hi i am getting this error when running this piece of code and cant figure it out. 嗨,我在运行这段代码时收到此错误,无法弄清楚。 cheers: 干杯:
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
The error is clear on what's going on: 该错误是明确的,这是怎么回事:
[index] non-negative and less than the size of the collection. [索引]非负且小于集合的大小。
Replace <=
with <
in the loop header: 替换<=
与<
在循环头:
for (int i = 0; i < (DataGridView1.Rows.Count); i++) {
...
}
In addition, you need to make sure that each row has at least three cells, because you are accessing the third cell in a row: Cells[2]
此外,您需要确保每行至少包含三个单元格,因为您要访问一行中的第三个单元格: Cells[2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.