简体   繁体   English

索引超出范围。 必须为非负数并且小于集合错误的大小

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

相关问题 错误:索引超出范围。 必须是非负的并且小于集合的大小 - Error: Index was out of range. Must be non-negative and less than the size of the collection 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection 错误C#:索引超出范围。 必须为非负数且小于集合的大小 - Error C#: Index was out of range. Must be non-negative and less than the size of the collection 索引超出范围。 必须为非负数并且小于Nhibernate中的集合错误大小 - Index was out of range. Must be non-negative and less than the size of the collection error in Nhibernate excpetion error:索引超出范围。 必须是非负数且小于集合的大小 - excpetion error :Index was out of range. Must be non-negative and less than the size of the collection 错误索引超出范围。 必须是非负数且小于集合的大小 - 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. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 (参数‘索引’)" - Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')" 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM