VB.Net程序设计:从DataGridView拖放一行数据到TreeView中的某个节点

拖放操作:从DataGridView拖放一行数据到TreeView中的某个节点。

拖动过程中会高亮鼠标所在的节点。

拖出时候会恢复节点正常样式。


    Dim dropNode As TreeNode

    Private Sub Dv_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Dv.MouseMove
        If (e.Button And Windows.Forms.MouseButtons.Left) = Windows.Forms.MouseButtons.Left Then
            Dim dvs As DataGridView = CType(sender, DataGridView)
            If dvs.HitTest(e.X, e.Y).Type = DataGridViewHitTestType.Cell Then
                If dvs.SelectedRows.Count > 0 Then
                    Dim drg As New DragDataObject(eDragType.MoveStaffToDept, dvs.SelectedRows.Item(0))
                    dvs.DoDragDrop(drg, DragDropEffects.Move)
                End If
            End If
        End If
    End Sub

    Private Sub UpdateStaffDepartmentID(intCompanyID As Integer, intDepartmentID As Integer, lngStaffID As Long)
       '...
    End Sub

    Private Sub Tv_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Tv.DragDrop
        If e.Data.GetDataPresent(GetType(DragDataObject)) Then
            Dim drg As DragDataObject = e.Data.GetData(GetType(DragDataObject))
            If drg.DragType = eDragType.MoveStaffToDept Then
                Dim tnode As TreeNode = Tv.GetNodeAt(Tv.PointToClient(Control.MousePosition))
                UpdateStaffDepartmentID(FindNodeCompanyID(tnode), tnode.Name, CLng(CType(drg.Data, DataGridViewRow).Cells(TBC.lngStaffID).Value))
                Me.Dv.Rows.Remove(CType(drg.Data, DataGridViewRow))
                If dropNode IsNot Nothing Then
                    EraserHighlightNode(dropNode)
                    dropNode = Nothing
                End If
            End If
        End If
    End Sub

    Private Sub Tv_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Tv.DragEnter
        'If e.Data.GetDataPresent(GetType(DragDataObject)) Then
        '    Dim drg As DragDataObject = e.Data.GetData(GetType(DragDataObject))
        '    If drg.DragType = eDragType.MoveStaffToDept Then
        '        e.Effect = DragDropEffects.All
        '    Else
        '        e.Effect = DragDropEffects.None
        '    End If
        'End If
    End Sub

    Private Sub Tv_DragLeave(sender As Object, e As System.EventArgs) Handles Tv.DragLeave
        If dropNode IsNot Nothing Then
            EraserHighlightNode(dropNode)
            dropNode = Nothing
        End If
    End Sub

    Private Sub HighlightNode(node As TreeNode)
        With node
            .BackColor = SystemColors.Highlight
            .ForeColor = SystemColors.HighlightText
        End With
    End Sub

    Private Sub EraserHighlightNode(node As TreeNode)
        With node
            .BackColor = SystemColors.Window
            .ForeColor = SystemColors.WindowText
        End With
    End Sub

    Private Sub Tv_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Tv.DragOver
        If e.Data.GetDataPresent(GetType(DragDataObject)) Then
            Dim drg As DragDataObject = e.Data.GetData(GetType(DragDataObject))
            If drg.DragType = eDragType.MoveStaffToDept Then
                Dim tnode As TreeNode = Tv.GetNodeAt(Tv.PointToClient(Control.MousePosition))
                If tnode IsNot Nothing Then
                    If tnode.Tag = eTreeTag.Department Then
                        e.Effect = DragDropEffects.All
                    Else
                        e.Effect = DragDropEffects.None
                    End If
                Else
                    e.Effect = DragDropEffects.None
                End If
                If e.Effect = DragDropEffects.None Then
                    If dropNode IsNot Nothing Then
                        EraserHighlightNode(dropNode)
                        dropNode = Nothing
                    End If
                Else
                    If tnode IsNot Nothing Then
                        If dropNode IsNot Nothing Then
                            If tnode.Equals(dropNode) = False Then
                                EraserHighlightNode(dropNode)
                                dropNode = tnode
                                HighlightNode(dropNode)
                            End If
                        Else
                            dropNode = tnode
                            HighlightNode(dropNode)
                        End If
                    End If
                End If
            Else
                e.Effect = DragDropEffects.None
            End If
        End If
    End Sub



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值