2010-07-21 115 views
1

我無法從剪貼板中讀取粘貼的datagridviewrow對象。我想要做的是,當用戶選擇並複製整行時,我會將該行作爲DataObject粘貼到剪貼板中。這工作得很好,但是當我嘗試讀取DataObject時(在用戶單擊粘貼之後),保存在剪貼板中的DataGridViewRow值始終爲Nothing。請幫忙!如何從剪貼板中檢索DataGridView? (它最終在剪貼板中爲空)

這是我用於複製和粘貼的代碼。

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 

回答

0

這是我用:

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 

我也將數據網格視圖的clipboardCopyMode屬性設置爲EnableAlwaysIncludeHeaderText。

我想複製他們選擇的任何單元格。

HTH

0

從我可以告訴剪貼板是不能夠採取的DataGridViewRow格式。這就是爲什麼你沒有得到任何回報。

試試這個:

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 

您也可以點擊這裏(它的C#,但其概念是相同的):Row copy/paste functionality in DataGridview(windows application)