Copy/Paste 在 DataGridView 中
Copy/Paste within DataGridView
我正在尝试在 DataGridView 中进行复制和粘贴,以实现与 Excel 类似的操作。我当前的代码执行此操作,但第一个单元格除外,它似乎将剪贴板中的所有内容粘贴到第一个单元格中。下面是我在 cell_keydown
事件中使用的代码。
澄清一下,如果我复制以下内容:
我得到结果:
粘贴的数据在单击单元格之前的两个日期之间确实有 space。
如果有人有更好的方法来完成我最终想做的事情,我也将不胜感激!
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
最好为 copy/paste 例程创建方法,这样您就可以从代码中的不同位置调用它们,例如单击按钮、按下按键、单击菜单项...等等
我认为 Copy
部分没有问题,因为 GetClipboardContent
函数可以完成这项工作。至于 Paste
部分,以下代码片段从剪贴板获取数据并粘贴从 CurrentCell 开始的选定单元格范围的值。超出范围的单元格被修剪。
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
从 DGV.KeyDown 事件中调用它们,例如如下所示:
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 中进行复制和粘贴,以实现与 Excel 类似的操作。我当前的代码执行此操作,但第一个单元格除外,它似乎将剪贴板中的所有内容粘贴到第一个单元格中。下面是我在 cell_keydown
事件中使用的代码。
澄清一下,如果我复制以下内容:
我得到结果:
粘贴的数据在单击单元格之前的两个日期之间确实有 space。
如果有人有更好的方法来完成我最终想做的事情,我也将不胜感激!
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
最好为 copy/paste 例程创建方法,这样您就可以从代码中的不同位置调用它们,例如单击按钮、按下按键、单击菜单项...等等
我认为 Copy
部分没有问题,因为 GetClipboardContent
函数可以完成这项工作。至于 Paste
部分,以下代码片段从剪贴板获取数据并粘贴从 CurrentCell 开始的选定单元格范围的值。超出范围的单元格被修剪。
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
从 DGV.KeyDown 事件中调用它们,例如如下所示:
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