Click here to Skip to main content
15,033,147 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to copy selected rows from ine datagrid to a second, this code copies only the index number :
VB
For Each drr As DataGridViewRow In DataGridView1.SelectedRows

    Form4.DataGridView1.Rows.Add(drr.Clone)

Next


Any help ty
Posted
Updated 15-Feb-17 8:23am

You are near, do as below
VB
For Each drr As DataGridViewRow In DataGridView1.SelectedRows
   Dim row As DataGridViewRow = CType(drr.Clone(), DataGridViewRow)
   For i As Int32 = 0 To drr.Cells.Count - 1
      row.Cells(i).Value = drr.Cells(i).Value
   Next
   Form4.DataGridView1.Rows.Add(row)
Next
   
'Hope This helps DGV1 is the datagridview
'To copy Row
Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
End Sub

'To Paste Row
Private Sub PasteButton_Click(sender As System.Object, e As System.EventArgs) Handles PasteButton.Click
    PasteRowIndex = DGV1.CurrentRow.Index
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(PasteRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next

End Sub

'To Duplicate Rows
Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
    DGV1.Rows.Add()
    DuplicateRowIndex = DGV1.Rows.Count - 1
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(DuplicateRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next
End Sub
   
Comments
Richard Deeming 15-Feb-17 15:05pm
   
Three years too late.
Member 13724938 14-Mar-18 4:03am
   
not for me

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