vb.net 错误=索引超出范围。必须是非负的并且小于集合的大小。参数名称:索引”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12997178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:00:07  来源:igfitidea点击:

error=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

vb.net

提问by user1712552

I have a gridview which when I upload my data and make an attempt to submit it to mysql database I get his error

我有一个 gridview,当我上传数据并尝试将其提交到 mysql 数据库时,我收到了他的错误

"error=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index""

“错误=索引超出范围。必须为非负且小于集合的大小。参数名称:索引”“

Here is the code I am using, and the problem occurs on the address row.

这是我正在使用的代码,问题出现在地址行上。

   For i = 0 To GridView1.Rows.Count - 1
            Using sqlCommand As New MySqlCommand()

                student_id = GridView1.Rows(0).Cells(i).Text.ToString
                age = GridView1.Rows(1).Cells(i).Text.ToString
               adress = GridView1.Rows(2).Cells(i).Text.ToString

回答by Steve

Something seems wrong here.
You loop over the rows of the gridview, but then you take the index of the row to access the cells.
Probably you want this

这里似乎有些不对劲。
您遍历 gridview 的行,然后获取行的索引以访问单元格。
可能你想要这个

For i = 0 To GridView1.Rows.Count - 1 
    Using sqlCommand As New MySqlCommand() 
       .... 
       student_id = GridView1.Rows(i).Cells(0).Text.ToString 
       age = GridView1.Rows(i).Cells(1).Text.ToString 
       adress = GridView1.Rows(i).Cells(2).Text.ToString 
       ....
    End Using
Next