Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VS2003. Copy and Paste row/s within DataGrid

VS2003. Copy and Paste row/s within DataGrid

Scheduled Pinned Locked Moved Visual Basic
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EvoFreak
    wrote on last edited by
    #1

    Hi, I have got this issue where I need to copy/paste multiple rows. At the moment, copying and pasting a single row seems possible. And it seems to work. copying and pasting multiple rows seems to be the problem. Only the last row that was highlighted to be copied seems to be entered within ClipBoard. Here is my code: Try If e.KeyCode = Keys.C Then 'copy key c is pressed Dim counter As Integer = 0 Dim selectedItemsArrayList As New ArrayList For Each r As DataStructure.ItemsRow In MainForm.Data.Items If dg.IsSelected(counter) Then selectedItemsArrayList.Add(counter) End If counter += 1 Next Dim format As DataFormats.Format = DataFormats.GetFormat(Me.ToString) Dim data As IDataObject = New DataObject Dim row As DataRow If selectedItemsArrayList.Count > 0 Then 'if count is greater than 0, there are some rows selected For Each i As Integer In selectedItemsArrayList 'for each row that is selected to be added 'add to clipboard selectedDataGridRow = selectedItemsArrayList(i) row = MainForm.Data.Items(selectedItemsArrayList(i)) data.SetData(format.Name, False, row.ItemArray) Clipboard.SetDataObject(data, False) Next End If End If If e.KeyCode = Keys.V Then 'paste key is pressed Dim row As Object() Dim ClipData As IDataObject = Clipboard.GetDataObject Dim format As String = Me.ToString If ClipData.GetDataPresent(format) Then row = CType(ClipData.GetData(format), Object()) 'add row to datagrid MainForm.Data.Items.Rows.Add(row) End If End If Catch ex As Exception MessageBox.Show(ex.ToString) End Try I guess I need to either modify the way I add items into ClipBoard or modify the way I retrieve items from within ClipBoard. Cheers

    --------------------------------- There is life outside coding.

    N 1 Reply Last reply
    0
    • E EvoFreak

      Hi, I have got this issue where I need to copy/paste multiple rows. At the moment, copying and pasting a single row seems possible. And it seems to work. copying and pasting multiple rows seems to be the problem. Only the last row that was highlighted to be copied seems to be entered within ClipBoard. Here is my code: Try If e.KeyCode = Keys.C Then 'copy key c is pressed Dim counter As Integer = 0 Dim selectedItemsArrayList As New ArrayList For Each r As DataStructure.ItemsRow In MainForm.Data.Items If dg.IsSelected(counter) Then selectedItemsArrayList.Add(counter) End If counter += 1 Next Dim format As DataFormats.Format = DataFormats.GetFormat(Me.ToString) Dim data As IDataObject = New DataObject Dim row As DataRow If selectedItemsArrayList.Count > 0 Then 'if count is greater than 0, there are some rows selected For Each i As Integer In selectedItemsArrayList 'for each row that is selected to be added 'add to clipboard selectedDataGridRow = selectedItemsArrayList(i) row = MainForm.Data.Items(selectedItemsArrayList(i)) data.SetData(format.Name, False, row.ItemArray) Clipboard.SetDataObject(data, False) Next End If End If If e.KeyCode = Keys.V Then 'paste key is pressed Dim row As Object() Dim ClipData As IDataObject = Clipboard.GetDataObject Dim format As String = Me.ToString If ClipData.GetDataPresent(format) Then row = CType(ClipData.GetData(format), Object()) 'add row to datagrid MainForm.Data.Items.Rows.Add(row) End If End If Catch ex As Exception MessageBox.Show(ex.ToString) End Try I guess I need to either modify the way I add items into ClipBoard or modify the way I retrieve items from within ClipBoard. Cheers

      --------------------------------- There is life outside coding.

      N Offline
      N Offline
      nlarson11
      wrote on last edited by
      #2

      The reason why you are only getting the last row is because of the clipboard works. it only retains one setdata at a time. it will not append automatically for you. so you need to bundle all your rows and use setdata once.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups