如何从剪贴板检索 DataGridView? (它最终在剪贴板中为空)

发布于 2024-09-10 16:09:10 字数 979 浏览 3 评论 0原文

我无法从剪贴板读取粘贴的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤还要旧人安 2024-09-17 16:09:10

这就是我使用的:

    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。

我想复制他们选择的任何单元格。

华泰

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

宛菡 2024-09-17 16:09:10

据我所知,剪贴板无法采用 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#,但概念是相同的): 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:

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文