Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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-----
VB
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
Posted
Updated 8-Mar-16 3:52am
v4
Comments
Andy Lanng 8-Mar-16 9:47am    
Please don't link to un-certified files. We discourage such linking to keep the minority of dangerous files away from the unwitting majority.

Your description should be enough. If it isn't then amend the post

Thanks ^_^
ionMEMBER 8-Mar-16 15:45pm    
Table on access is with 2 columns: "ID and Description". Whats the right code to copy the value from DataGridview1 to DataGridview2 ?
CHill60 8-Mar-16 9:49am    
On which line does the error occur?
Andy Lanng 8-Mar-16 9:52am    
I guessed the line with 'cell(2).Value'
CHill60 8-Mar-16 10:07am    
So did I ... was just making a point :)

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

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Mar-16 13:46pm    
5ed.
—SA
Sascha Lefèvre 8-Mar-16 13:54pm    
Thank you, Sergey.
The hard-coded reference must be the problem:
VB
Dim row As String() = New String() {cell(0).Value, cell(1).Value, cell(2).Value}


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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900