views:

121

answers:

2

I am unable to read a pasted datagridviewrow object from the clipboard. All I want to do is, when a user has the entire row selected and copied, I would paste that row into the clipboard as a DataObject. That works just fine but when I attempt to read that DataObject (after the user clicks Paste) the DataGridViewRow that's saved in the clipboard always has a value of Nothing. Please help!

Here's the code I'm using for Copy and Paste.

Private Sub copyToClipboard()
    If DataGridView1.CurrentCell IsNot Nothing AndAlso _
        DataGridView1.CurrentCell.Value IsNot Nothing Then
        If DataGridView1.SelectedRows.Count = 1 Then
            My.Computer.Clipboard.SetData(GetType(DataGridViewRow).ToString, getActiveGrid.SelectedRows(0))
        End If
    End If
End Sub

Private Sub pasteFromClipboard()
    Dim oDataObject As IDataObject = My.Computer.Clipboard.GetDataObject
    If oDataObject.GetDataPresent(GetType(DataGridViewRow).ToString) Then
        Dim GridRow As DataGridViewRow = _
            DirectCast(oDataObject.GetData(GetType(DataGridViewRow).ToString), DataGridViewRow)
        ' here's the issue. the variable GridRow always has a value of nothing. 
    End If
End Sub
A: 

From what I can tell the Clipboard is not able take the DataGridViewRow format. That's why you are not getting anything back.

Try This:

Private Sub copyToClipboard()
If DataGridView1.CurrentCell IsNot Nothing AndAlso _
    DataGridView1.CurrentCell.Value IsNot Nothing Then
    If DataGridView1.SelectedRows.Count = 1 Then
        My.Computer.Clipboard.SetDataObject(getActiveGrid.GetClipboardContent())
    End If
End If
End Sub

Private Sub pasteFromClipboard()
Dim oDataObject As IDataObject = My.Computer.Clipboard.GetDataObject
If oDataObject.GetDataPresent(DataFormats.Text) Then
   Dim strRow as String = Clipboard.GetData(DataFormats.Text)

   'Then create a datagridrow using the data       

End If
End Sub

You can also check here (it's C#, but the concepts are the same): Row copy/paste functionality in DataGridview(windows application)

Tony Abrams
A: 

This is what I use:

    Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
    If dgvDisplaySet.GetClipboardContent Is Nothing Then
        MsgBox("Nothing selected to copy to clipboard.")
    Else
        Clipboard.SetDataObject(dgvDisplaySet.GetClipboardContent)
    End If
End Sub

I also have the data grid view's clipboardCopyMode property set to EnableAlwaysIncludeHeaderText.

I want to copy whatever cells they have selected.

HTH

Beth