Dewayne Pinion
Dewayne Pinion

Reputation: 41

Trying to copy a datagridview from one form to another

I know my title is a little strange. Let me explain. I am trying to "merge" two databases, one we house locally and one that is at another facility with the hopes of avoiding our admin group having to duplicate entry through separate forms. I have the source for a webservice that connects to the offsite database and I can enter data. What I am trying to do is expand that source by adding in our own database and copying our data to theirs. Hope this makes sense.

Anyway,

So both the local and offsite forms have datagridviews attached to them. I can currently copy the contents of the first row to their form for entry, but I am getting errors when it attempts to go to the second row. Here is my code:

  Dim i As Integer
        For i = 0 To VRMAdDataGridView.Rows.Count - 1
            'For Each row As DataGridViewRow In VRMAdDataGridView.Rows

            'myrow = VRMAdDataGridView.CurrentRow.Index
            frmcall.dgvRMAItems.Rows(i).Cells("cItemID").Value = Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn5").Value
            frmcall.dgvRMAItems.Rows(i).Cells("cExpectedSerialNumber").Value = Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn2").Value
            frmcall.dgvRMAItems.Rows(i).Cells("cNotes").Value = Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn3").Value

            'Format Type codes from FW to match Utica
            Select Case Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn6").Value
                Case "Repair"
                    frmcall.dgvRMAItems.Rows(i).Cells("cRequestedAction").Value = "Repair"
                Case "RTS"
                    frmcall.dgvRMAItems.Rows(i).Cells("cRequestedAction").Value = "Rtn/Stock"
                Case "Repair !!RUSH!!"
                    frmcall.dgvRMAItems.Rows(i).Cells("cRequestedAction").Value = "Rush Repair"

            End Select


            ' myrowval = row.Cells("DataGridViewTextBoxColumn5").Value
            ' MsgBox(myrowval, MsgBoxStyle.Information, "Prod_Code")
            'Do Something
        Next

When I try to run the code I receive the following error:

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

Upvotes: 0

Views: 1119

Answers (1)

Victor Zakharov
Victor Zakharov

Reputation: 26424

You should manipulate the underlying datasources, not UI elements (DataGridViews in this case). For example, your DataSource is a DataTable. Then just use Clone method to get a copy. Then set a DataSource of that other DataGridView. If you need something more than a copy, it's definitely going to be more simple to work with a series of DataRow objects.

Upvotes: 1

Related Questions