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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的VB相关文章