从第一个DATAGRIDVIEW 拖动数据到第二个DATAGRIDVIEW 进行数据覆盖

wx9288 2017-03-20 10:57:10
有两个DATAGRIDVIEW控件 两个里面都有数据

第一个DATAGRIDVIEW 1 控件 数据如下

标题 内容
1 A
2 AA
3 AAA
...... .......


第二个DATAGRIDVIEW 2控件 数据如下

资料 文档 标题 内容
X1 CCA 空 空
F2 FFA 空 空
..... ....... ..... .....


需实现目标如下

例如 将 DATAGRIDVIEW 1 的某一行
例如 1 A

拖动到
DATAGRIDVIEW 2 的任意一行 DATAGRIDVIEW 1 的数据 会追加到 DATAGRIDVIEW 2 里面


求实现效果
资料 文档 标题 内容
X1 CCA 1 A

F2 FFA 1 A





...全文
266 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiger_Zhao 2017-03-20
  • 打赏
  • 举报
回复
'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

放下行的定位已经实现。
你自己用HitTest来实现拖出行的定位(现在用了选中行)。

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧