【问题标题】:How can I retrieve a DataGridView from the clipboard? (It ends up being Null in clipboard)如何从剪贴板中检索 DataGridView? (它最终在剪贴板中为 Null)
【发布时间】:2010-07-21 17:14:10
【问题描述】:

我无法从剪贴板中读取粘贴的 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

【问题讨论】:

    标签: c# .net vb.net datagridview clipboard


    【解决方案1】:

    这是我用的:

        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

    【讨论】:

      【解决方案2】:

      据我所知,剪贴板无法采用 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)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多