VS2003. Copy and Paste row/s within DataGrid
-
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.
-
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.