我的项目有问题! vb.net [英] I have a problem with my project ! vb.net

查看:67
本文介绍了我的项目有问题! vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指数超出范围。必须是非负数且小于集合的大小。

参数名称:index

我的项目链接:

<< link removed>>





----代码-----

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
MY PROJECT LINK:
<<link removed>>


----CODE-----

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the DatabDataSet.tabel2 table. You can move, or remove it, as needed.
        Me.Tabel2TableAdapter.Fill(Me.DatabDataSet.tabel2)

    End Sub

    Private Sub COPY(ByVal source As DataGridView, ByVal des As DataGridView)
        For Each rows In source.SelectedRows
            Dim cell As DataGridViewCellCollection = rows.cells
            Dim row As String() = New String() {cell(0).Value, cell(1).Value, cell(2).Value}
            des.Rows.Add(row)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        COPY(DataGridView1, Form1.DataGridView1)
        Me.Hide()
    End Sub
End Class





我尝试了什么:



当我创建没有数据的列时它工作但当我尝试访问此数据的数据时显示



What I have tried:

When I create columns without data its working but when I try the data with access this problem show

推荐答案

此代码中唯一使用索引的地方是这一行:

The only place you use indices in this code is this line:
Dim row As String() = New String() {cell(0).Value, cell(1).Value, cell(2).Value}



所以你的错误没有其他可能的原因,一行中没有三个单元格。也许它只是两个或一个(甚至没有)。如果你打算总是有三个单元格,那么你的源DataGridView没有正确构造,你必须检查那里的代码。否则你应该在这里修改你的代码,以便能够处理可变数量的单元格,通过使用for循环将值复制到一个你预先使用单元格计数的数组中。


So there's no other possible reason for your error that there aren't three cells in one row. Maybe it's just two or one (or even none). If you intended that there always be exactly three cells then your source DataGridView wasn't correctly constructed and you have to check the code there. Otherwise you should modify your code here to be able to deal with a variable amount of cells, by using a for-loop to copy over the values into an array which you 'dim' with the cell-count beforehand.


硬编码引用必须是问题:

The hard-coded reference must be the problem:
Dim row As String() = New String() {cell(0).Value, cell(1).Value, cell(2).Value}





您需要知道如何处理错误。要么遍历for [each]循环中的每个项目,要么检查单元格数组包含那么多项目。





您会发现 cell 数组为空或少于3个项目。



当cell.Length小于3时调用单元格(2)会抛出该错误



You need to know how to handle errors. Either go through each item in a for[each] loop or check the the cell array contains that many items.


You will find that cell array is either empty or has fewer than 3 items.

Calling cell(2) when cell.Length is less than 3 will throw that error


这篇关于我的项目有问题! vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆