【问题标题】:Copy and paste with a WPF dataGrid使用 WPF 数据网格复制和粘贴
【发布时间】:2016-07-05 08:49:26
【问题描述】:

我正在使用 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

有什么提示吗?

【问题讨论】:

    标签: wpf vb.net datagrid copy-paste


    【解决方案1】:

    这是一个基本的例子:

    我创建了一个带有按钮的数据网格视图。

    按钮循环选择的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
    

    【讨论】:

    • 非常感谢您的回复。这是针对 WPF 的吗?我得到 DataGridViewCell 未定义,目前我使用的是 dataGrid 而不是 dataGridView。
    猜你喜欢
    • 2011-05-06
    • 2011-09-15
    • 1970-01-01
    • 2021-01-19
    • 2011-11-25
    • 2011-02-08
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多