16,553
社区成员
发帖
与我相关
我的任务
分享
'Form1
'放两个 DataGridView
Public Class Form1
Private Function DropTest(ByVal e As System.Windows.Forms.DragEventArgs, ByRef rowIndex As Integer) As Boolean
If e.Data.GetDataPresent(GetType(DataGridViewRow)) Then
Dim pt As Point = Me.DataGridView2.PointToClient(New Point(e.X, e.Y))
Dim info As DataGridView.HitTestInfo = Me.DataGridView2.HitTest(pt.X, pt.Y)
'Debug.Print(String.Format("DropTest ({0},{1}) {2} {3}", pt.X, pt.Y, info.Type, info.RowIndex))
If info.Type = DataGridViewHitTestType.Cell Then
rowIndex = info.RowIndex
e.Effect = DragDropEffects.Copy
Return True
End If
End If
e.Effect = DragDropEffects.None
Return False
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.DataGridView1.Columns.Add("col3", "标题")
Me.DataGridView1.Columns.Add("col4", "内容")
Me.DataGridView1.Rows.Add(1, "A")
Me.DataGridView1.Rows.Add(2, "AA")
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.ReadOnly = True
Me.DataGridView2.Columns.Add("col1", "资料")
Me.DataGridView2.Columns.Add("col2", "文档")
Me.DataGridView2.Columns.Add("col3", "标题")
Me.DataGridView2.Columns.Add("col4", "内容")
Me.DataGridView2.Rows.Add("X1", "CCA")
Me.DataGridView2.Rows.Add("F2", "FFA")
Me.DataGridView2.AllowUserToAddRows = False
Me.DataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.DataGridView2.AllowDrop = True
End Sub
Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
If Me.DataGridView1.SelectedRows.Count <> 0 Then
Me.DataGridView1.DoDragDrop(Me.DataGridView1.SelectedRows(0), DragDropEffects.Copy)
End If
End If
End Sub
Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop
Dim rowIndex As Integer = 0
If Me.DropTest(e, rowIndex) Then
Dim row1 As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
Dim row2 As DataGridViewRow = Me.DataGridView2.Rows(rowIndex)
row2.Cells("col3").Value = row1.Cells("col3").Value
row2.Cells("col4").Value = row1.Cells("col4").Value
End If
End Sub
Private Sub DataGridView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragEnter
Dim rowIndex As Integer = 0
Me.DropTest(e, rowIndex)
End Sub
Private Sub DataGridView2_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragOver
Dim rowIndex As Integer = 0
Me.DropTest(e, rowIndex)
End Sub
End Class