使用WPF dataGrid进行复制和粘贴

时间:2016-07-05 08:49:27

标签: wpf vb.net datagrid copy-paste

我正在使用WPF和Visual Basic。我试图有一个允许从我拥有的DataGrid复制的按钮。我所做的一切似乎都不允许我访问所选数据,并且只使用触发DataGrid上的复制事件的替代方法,我不知道如何操作。这是一个这样的失败代码示例:

Private Sub btnFECopy_Click(sender As Object, e As RoutedEventArgs) Handles btnFECopy.Click
    Dim text As New System.Text.StringBuilder()

    For Each row As DataGridCellInfo In dgFileEditorWindow.SelectedCells
        For Each cell As System.Data.DataRowView In row.Item
            If Not text.Equals("") Then
                text.Append(";")
            End If
            text = text.Append(cell.ToString)
            'MsgBox(cell.Column.DisplayIndex)
            'MsgBox(dataGrid1.Items.IndexOf(cell.Item))
        Next
    Next

    'Nothing text.Append(dgFileEditorWindow.CurrentCell.Item.ToString())

    'Nothing Console.WriteLine(dgFileEditorWindow.CurrentCell.ColumnNumber & " " & dgFileEditorWindow.CurrentCell.RowNumber)

        Clipboard.SetText(text.ToString())
End Sub

任何提示?

1 个答案:

答案 0 :(得分:0)

这是一个基本的例子:

我用按钮创建了一个datagridview。

按钮循环选定的datagridviewcells

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim text As New System.Text.StringBuilder()

    For Each dgvr As DataGridViewCell In DataGridView1.SelectedCells
        If Not Text.Equals("") Then
            Text.Append(";")
        End If

    Next

    Clipboard.SetText(text.ToString())
End Sub