Copy selected rows from unbound datagridview and reinsert them in the same datagridview - VB.Net (WinForms)

Hi!
I have one datagridview, which I fill with code cell by cell.
I want in runtime to create a copy of curently selected rows and reinsert this rows just before the current selection in the same datagridview with the same content.
In this datagridview have 2 columns which are dropdowns in 5 and 7 column.
What is the best way and some code for that?
LVL 1
Observer_123Asked:
Who is Participating?
 
Jorge PaulinoIT Pro/DeveloperCommented:
Ok then try this snippet.
It's the same for comboboxcolumn then the cells. The comboboxes stores the value in the cell.

        For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
            Me.DataGridView1.Rows.Add(New Object() _
                    {row.Cells(0).Value, _
                     row.Cells(1).Value, _
                     row.Cells(2).Value, _
                     row.Cells(3).Value, _
                     row.Cells(4).Value, _
                     row.Cells(5).Value, _
                     row.Cells(6).Value})
        Next

Open in new window

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Hi,
You can do something like this (you may need to change something).
I have used the keydown event for CTRL+V combination

    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.Control AndAlso e.KeyCode = Keys.V Then
 
            Try
                For Each line As String In Clipboard.GetText.Split(vbNewLine)
                    Dim item() As String = line.Trim.Split(vbTab)
 
                    If item.Length = Me.DataGridView1.ColumnCount Then
                        Me.DataGridView1.Rows.Add(New Object() {item(0), item(1), item(2), item(3), item(4)})
                    End If
                Next
 
            Catch ex As Exception
                MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
 
        End If
    End Sub

Open in new window

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Opps, this is for 4 columns ... just change it to the required ones.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Observer_123Author Commented:
Hi!
The copy from clipboard is interesting, but it could be enought just duplication - we have selected rows , click a button and they are inserted. Is there some issue with dropdown copumns, or copy them as regular cells?
0
 
Observer_123Author Commented:
the last one looks mostly what I need, but in this case may it will be added at the end of datagridview.
any idea how to insert them just below the last selected row?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Just change it to:
Me.DataGridView1.Rows.Insert(row.Index + 1, New Object() _
                    {row.Cells(0).Value, _
                    ...

Open in new window

0
 
Observer_123Author Commented:
Thanxs:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.