如何从剪贴板检索 DataGridView? (它最终在剪贴板中为空)
我无法从剪贴板读取粘贴的 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我使用的:
我还将数据网格视图的 ClipboardCopyMode 属性设置为 EnableAlwaysIncludeHeaderText。
我想复制他们选择的任何单元格。
华泰
This is what I use:
I also have the data grid view's clipboardCopyMode property set to EnableAlwaysIncludeHeaderText.
I want to copy whatever cells they have selected.
HTH
据我所知,剪贴板无法采用 DataGridViewRow 格式。这就是为什么你没有得到任何回报。
试试这个:
您也可以在这里检查(它是 C#,但概念是相同的): DataGridview 中的行复制/粘贴功能(Windows 应用程序)
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:
You can also check here (it's C#, but the concepts are the same): Row copy/paste functionality in DataGridview(windows application)