• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

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?
0
Observer_123
Asked:
Observer_123
  • 4
  • 3
2 Solutions
 
jpaulinoCommented:
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
 
jpaulinoCommented:
Opps, this is for 4 columns ... just change it to the required ones.
0
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jpaulinoCommented:
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
 
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
 
jpaulinoCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now