在DataGridView中复制/粘贴 [英] Copy/Paste within DataGridView

查看:507
本文介绍了在DataGridView中复制/粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在DataGridView中进行复制和粘贴,以使其类似于Excel。我当前的代码执行此操作,但第一个单元格除外,该单元格似乎将剪贴板中的所有内容粘贴到第一个单元格中。下面是我在 cell_keydown 事件中使用的代码。



请澄清一下,如果我复制以下内容:





我得到结果:





粘贴的数据,在单击单元格之前,两个日期之间确实有一个空格。



如果有人能更好地完成我最终尝试做的事情,那也将不胜感激!



< pre class = lang-vb prettyprint-override> 如果e.Control AndAl。e.KeyCode = Keys.C然后
Dim d As DataObject = dgv1.GetClipboardContent()
剪贴板.SetDataObject(d)
e.Handled = True

ElseIf e.Control AndAlso e.KeyCode = Keys.V然后
Dim s作为String = Clipboard.GetText()。 Replace(vbCr,)
Dim行As String()= s.Trim.Split(vbLf)
Dim行As Integer = dgv1.CurrentCell.RowIndex
Dim col As Integer = dgv1 .CurrentCell.ColumnIndex
Dim linesCount As Integer = lines.Count()

If(row + linesCount)-dgv1.RowCount> 0然后dgv1.Rows.Add((row + linesCount)-dgv1.RowCount)

对于每一行作为字符串在行
中如果line.Length> 0然后
Dim单元格为String()= line.Split(vbTab)
For i As Integer = 0到cells.GetLength(0)-1
dgv1.CurrentCell.Value = cells( i)
如果col + i< dgv1.ColumnCount然后
dgv1(col + i,row).Value =单元格(i)
其他
退出
结束如果
下一个
行+ = 1
其他

退出,如果
,下一个
结束,如果


解决方案

更好地为复制/粘贴例程创建方法,以便您可以从代码中的不同位置调用它们,例如单击按钮,按下按钮时,在菜单项上单击...等。



我认为您对复制没问题部分,因为 GetClipboardContent 函数将完成此工作。对于粘贴部分,以下代码段从剪贴板获取数据,并粘贴从CurrentCell开始的单元格选择范围的值。超出范围的单元格将被修剪。

  Private Sub CopyCells()
剪贴板。 SetDataObject(DataGridView1.GetClipboardContent)
End Sub

Private Sub PasteCells()
Dim s = Clipboard.GetText
Dim ci = DataGridView1.CurrentCell.ColumnIndex
Dim ri = DataGridView1.CurrentCell.RowIndex
Dim colCount = DataGridView1.Columns.Count
Dim rowCount = DataGridView1.Rows.Count

每个r in s.Split({ ControlChars.CrLf},StringSplitOptions.None)
暗单元格= ci
每个c in r.Split({ControlChars.Tab},StringSplitOptions.None)
如果Cell> = colCount然后退出
DataGridView1(Cell,ri).Value = c
单元格+ = 1
下一个
ri + = 1
如果ri> = rowCount然后退出
下一个
结束子

从DGV.KeyDown事件中调用它们以进行检查请如下所示:

  Private Sub DataGridView1_KeyDown(sender as object,e as KeyEventArgs)处理DataGridView1。 KeyDown 
如果使用e.Control然后
选择大小写e.KeyCode
Case Keys.C
CopyCells()
e.Handled = True
Case Keys。 V
PasteCells()
e.Handled = True
End选择
End if
End Sub


I am trying to make copy and pasting within a DataGridView to act similarly to Excel. My current code performs this with the exception of the first cell, which seems to be pasting all contents from the clipboard into the first cell. Below is the code I am using on the cell_keydown event.

Just to clarify, if I copy the following:

I get the result:

The pasted data, does have a space between the two dates prior to clicking off the cell.

If anyone has a better way to accomplish what I am ultimately trying to do that would be appreciated as well!

If e.Control AndAlso e.KeyCode = Keys.C Then
    Dim d As DataObject = dgv1.GetClipboardContent()
    Clipboard.SetDataObject(d)
    e.Handled = True

ElseIf e.Control AndAlso e.KeyCode = Keys.V Then
    Dim s As String = Clipboard.GetText().Replace(vbCr, " ")
    Dim lines As String() = s.Trim.Split(vbLf)
    Dim row As Integer = dgv1.CurrentCell.RowIndex
    Dim col As Integer = dgv1.CurrentCell.ColumnIndex
    Dim linesCount As Integer = lines.Count()

    If (row + linesCount) - dgv1.RowCount > 0 Then dgv1.Rows.Add((row + linesCount) - dgv1.RowCount)

    For Each line As String In lines
        If line.Length > 0 Then
            Dim cells As String() = line.Split(vbTab)
            For i As Integer = 0 To cells.GetLength(0) - 1
                dgv1.CurrentCell.Value = cells(i)
                If col + i < dgv1.ColumnCount Then
                    dgv1(col + i, row).Value = cells(i)
                Else
                    Exit For
                End If
            Next
            row += 1
        Else
            Exit For
        End If
    Next
End If

解决方案

Better to create methods for the copy/paste routines so you can call them from different places in your code, like on button click, on key press, on menu item click ...etc.

I don't think you have a problem with the Copy part since the GetClipboardContent function will do the job. As for the Paste part, the following code snippet gets the data from the Clipboard and pastes the values of a selection range of cells starting from the CurrentCell. The out of range cells are trimmed.

Private Sub CopyCells()
    Clipboard.SetDataObject(DataGridView1.GetClipboardContent)
End Sub

Private Sub PasteCells()
    Dim s = Clipboard.GetText
    Dim ci = DataGridView1.CurrentCell.ColumnIndex
    Dim ri = DataGridView1.CurrentCell.RowIndex
    Dim colCount = DataGridView1.Columns.Count
    Dim rowCount = DataGridView1.Rows.Count

    For Each r In s.Split({ControlChars.CrLf}, StringSplitOptions.None)
        Dim Cell = ci
        For Each c In r.Split({ControlChars.Tab}, StringSplitOptions.None)
            If Cell >= colCount Then Exit For
            DataGridView1(Cell, ri).Value = c
            Cell += 1
        Next
        ri += 1
        If ri >= rowCount Then Exit For
    Next
End Sub

Call them from the DGV.KeyDown event for example as follows:

Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
    If e.Control Then
        Select Case e.KeyCode
            Case Keys.C
                CopyCells()
                e.Handled = True
            Case Keys.V
                PasteCells()
                e.Handled = True
        End Select
    End If
End Sub

这篇关于在DataGridView中复制/粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆