我无法从剪贴板中读取粘贴的datagridviewrow对象。我想要做的就是,当用户选择并复制整行时,我会将该行作为DataObject粘贴到剪贴板中。这很好但是当我尝试读取DataObject时(在用户单击“粘贴”之后),保存在剪贴板中的DataGridViewRow的值始终为Nothing。请帮忙!
这是我用于复制和粘贴的代码。
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 :(得分: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)
答案 1 :(得分: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