This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
/
GridManager.vb
157 lines (145 loc) · 6.96 KB
/
GridManager.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports DevExpress.XtraGrid
Imports System.Data
Imports DevExpress.XtraTreeList.Nodes
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports System.Drawing
Imports DevExpress.XtraTreeList
Namespace DragAndDropBetweenGridAndTreeList
Friend Class GridManager
Private ReadOnly _gridControl As GridControl
Private _dragStartHitInfo As GridHitInfo
Private _dragRowCursor As Cursor
Private ReadOnly _imageHelper As DragGridImageHelper
Public Sub New(ByVal grid As GridControl)
_gridControl = grid
SetUpGrid(grid)
_imageHelper = New DragGridImageHelper(TryCast(grid.FocusedView, GridView))
End Sub
Public Sub SetUpGrid(ByVal grid As GridControl)
grid.AllowDrop = True
AddHandler grid.DragOver, AddressOf grid_DragOver
AddHandler grid.DragDrop, AddressOf grid_DragDrop
AddHandler grid.DragLeave, AddressOf grid_DragLeave
AddHandler grid.Paint, AddressOf grid_Paint
AddHandler grid.GiveFeedback, AddressOf grid_GiveFeedback
Dim view As GridView = TryCast(grid.MainView, GridView)
view.OptionsBehavior.Editable = False
AddHandler view.MouseMove, AddressOf view_MouseMove
AddHandler view.MouseDown, AddressOf view_MouseDown
End Sub
Private Sub view_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim view As GridView = TryCast(sender, GridView)
_dragStartHitInfo = Nothing
Dim hitInfo As GridHitInfo = view.CalcHitInfo(New Point(e.X, e.Y))
If Control.ModifierKeys <> Keys.None Then
Return
End If
If e.Button = MouseButtons.Left AndAlso hitInfo.RowHandle >= 0 Then
_dragStartHitInfo = hitInfo
End If
End Sub
Private Sub view_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim view As GridView = TryCast(sender, GridView)
If e.Button = MouseButtons.Left AndAlso _dragStartHitInfo IsNot Nothing Then
Dim dragSize As Size = SystemInformation.DragSize
Dim dragRect As New Rectangle(New Point(_dragStartHitInfo.HitPoint.X - dragSize.Width \ 2, _dragStartHitInfo.HitPoint.Y - dragSize.Height \ 2), dragSize)
If Not dragRect.Contains(New Point(e.X, e.Y)) Then
_dragRowCursor = _imageHelper.GetDragCursor(_dragStartHitInfo.RowHandle, e.Location)
Dim row As DataRow = view.GetDataRow(_dragStartHitInfo.RowHandle)
view.GridControl.DoDragDrop(row, DragDropEffects.Move)
_dragStartHitInfo = Nothing
DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = True
End If
End If
End Sub
Private dropTargetRowHandle_Renamed As Integer = -1
Private Property DropTargetRowHandle() As Integer
Get
Return dropTargetRowHandle_Renamed
End Get
Set(ByVal value As Integer)
dropTargetRowHandle_Renamed = value
_gridControl.Invalidate()
End Set
End Property
Private Sub grid_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
Dim grid As GridControl = TryCast(sender, GridControl)
Dim table As DataTable = TryCast(grid.DataSource, DataTable)
Dim node As TreeListNode = TryCast(e.Data.GetData(GetType(TreeListNode)), TreeListNode)
If node.RootNode Is node Then
Return
End If
If node IsNot Nothing AndAlso table IsNot Nothing Then
Dim itemArray As New List(Of Object)()
For Each column As DataColumn In table.Columns
itemArray.Add(node.GetValue(column))
Next column
Dim tmp(itemArray.Count - 1) As Object
itemArray.CopyTo(tmp)
Dim row As DataRow = table.NewRow()
row.ItemArray = tmp
table.Rows.InsertAt(row, DropTargetRowHandle)
Dim treeList As TreeList = node.TreeList
treeList.DeleteNode(node)
End If
DropTargetRowHandle = -1
End Sub
Private Sub grid_DragOver(ByVal sender As Object, ByVal e As DragEventArgs)
Dim grid As GridControl = DirectCast(sender, GridControl)
Dim pt As New Point(e.X, e.Y)
pt = grid.PointToClient(pt)
Dim view As GridView = TryCast(grid.GetViewAt(pt), GridView)
If view Is Nothing Then
Return
End If
Dim hitInfo As GridHitInfo = view.CalcHitInfo(pt)
If hitInfo.RowHandle = GridControl.InvalidRowHandle Then
DropTargetRowHandle = view.DataRowCount
Else
DropTargetRowHandle = hitInfo.RowHandle
End If
Dim node As TreeListNode = TryCast(e.Data.GetData(GetType(TreeListNode)), TreeListNode)
If DropTargetRowHandle >= 0 AndAlso e.Data.GetDataPresent(GetType(TreeListNode)) AndAlso node.RootNode IsNot node Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub grid_DragLeave(ByVal sender As Object, ByVal e As EventArgs)
DropTargetRowHandle = -1
End Sub
Private Sub grid_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
If DropTargetRowHandle < 0 Then
Return
End If
Dim grid As GridControl = DirectCast(sender, GridControl)
Dim view As GridView = CType(grid.MainView, GridView)
Dim isBottomLine As Boolean = DropTargetRowHandle = view.DataRowCount
Dim viewInfo As GridViewInfo = TryCast(view.GetViewInfo(), GridViewInfo)
Dim rowInfo As GridRowInfo = viewInfo.GetGridRowInfo(If(isBottomLine, DropTargetRowHandle - 1, DropTargetRowHandle))
If rowInfo Is Nothing Then
Return
End If
Dim p1, p2 As Point
If isBottomLine Then
p1 = New Point(rowInfo.Bounds.Left, rowInfo.Bounds.Bottom - 1)
p2 = New Point(rowInfo.Bounds.Right, rowInfo.Bounds.Bottom - 1)
Else
p1 = New Point(rowInfo.Bounds.Left, rowInfo.Bounds.Top - 1)
p2 = New Point(rowInfo.Bounds.Right, rowInfo.Bounds.Top - 1)
End If
e.Graphics.DrawLine(Pens.Blue, p1, p2)
End Sub
Private Sub grid_GiveFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs)
If _dragStartHitInfo IsNot Nothing Then
e.UseDefaultCursors = False
Cursor.Current = _dragRowCursor
End If
End Sub
End Class
End Namespace