vb.net datagridview中选择范围,实现ctrl+c/ctrl+v
处理完成马上给分。 展开
拷贝模式设定
DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
选中部分拷贝
Clipboard.SetDataObject(DataGridView1.GetClipboardContent())
If DataGridView1.CurrentCell Is Nothing Then
Return
End If
Dim insertRowIndex As Integer = DataGridView1.CurrentCell.RowIndex
Dim pasteText As String = Clipboard.GetText()
If String.IsNullOrEmpty(pasteText) Then
Return
End If
pasteText = pasteText.Replace(vbCrLf, vbLf)
pasteText = pasteText.Replace(vbCr, vbLf)
pasteText.TrimEnd(New Char() {vbLf})
Dim lines As String() = pasteText.Split(vbLf)
Dim isHeader As Boolean = True
For Each line As String In lines
If isHeader Then
isHeader = False
Else
Dim vals As String() = line.Split(ControlChars.Tab)
If vals.Length - 1 <> DataGridView1.ColumnCount Then
Throw New ApplicationException("列数が违います。")
End If
Dim row As DataGridViewRow = DataGridView1.Rows(insertRowIndex)
row.HeaderCell.Value = vals(0)
Dim i As Integer
For i = 0 To row.Cells.Count - 1
row.Cells(i).Value = vals((i + 1))
Next i
insertRowIndex += 1
End If
Next line
哥,我要做的是在datagirdview中鼠标拖动,选择几行几列,进行复制粘贴。这个不行啊。
SendKeys.Send("^{C}") 发送CLTR+C命令-----复制
'中间延迟200ms左右
SendKeys.Send("^{v}") '向活动程序EXCEL发送CLTR+V命令-----粘贴