/
GridViewHelper.vb
121 lines (97 loc) · 4.87 KB
/
GridViewHelper.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
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Base.ViewInfo
Namespace WindowsApplication1
Public Class GridViewHelper
Private gridView As GridView
Private sourceGridCell As GridCell
Private lastMouseDownMoveCell As GridCell
Public Sub New(ByVal gridView As GridView)
Me.gridView = gridView
AddHandler gridView.MouseDown, AddressOf gridView_MouseDown
AddHandler gridView.MouseMove, AddressOf gridView_MouseMove
AddHandler gridView.MouseUp, AddressOf gridView_MouseUp
AddHandler gridView.CustomDrawCell, AddressOf gridView_CustomDrawCell
End Sub
Private Sub gridView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim view As BaseView = gridView.GridControl.GetViewAt(e.Location)
TryCast(view, GridView).GridControl.Cursor = Cursors.Default
Dim baseHI As BaseHitInfo = view.CalcHitInfo(e.Location)
Dim gridHI As GridHitInfo = TryCast(baseHI, GridHitInfo)
If gridHI.HitTest = GridHitTest.RowEdge Then
sourceGridCell = Nothing
lastMouseDownMoveCell = Nothing
baseHI = view.CalcHitInfo(New Point(e.X, e.Y - 3))
gridHI = TryCast(baseHI, GridHitInfo)
If gridHI.HitTest = GridHitTest.RowCell Then
Dim val As Object = (TryCast(view, GridView)).GetRowCellValue(gridHI.RowHandle, gridHI.Column)
If val Is Nothing OrElse val.ToString() = String.Empty Then
Return
End If
sourceGridCell = New GridCell(gridHI.RowHandle, gridHI.Column)
lastMouseDownMoveCell = New GridCell(sourceGridCell.RowHandle, sourceGridCell.Column)
End If
End If
End Sub
Private Sub gridView_CustomDrawCell(ByVal sender As Object, ByVal e As RowCellCustomDrawEventArgs)
If lastMouseDownMoveCell Is Nothing OrElse sourceGridCell Is Nothing Then
Return
End If
If e.RowHandle = lastMouseDownMoveCell.RowHandle AndAlso e.Column Is lastMouseDownMoveCell.Column Then
e.Cache.DrawRectangle(e.Cache.GetPen(Color.Gray), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
End If
End Sub
Private Sub gridView_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim view As BaseView = gridView.GridControl.GetViewAt(e.Location)
Dim baseHI As BaseHitInfo = view.CalcHitInfo(e.Location)
Dim gridHI As GridHitInfo = TryCast(baseHI, GridHitInfo)
If gridHI.HitTest = GridHitTest.RowCell AndAlso sourceGridCell IsNot Nothing Then
Dim is_changed As Boolean = False
If lastMouseDownMoveCell.RowHandle <> gridHI.RowHandle OrElse lastMouseDownMoveCell.Column.FieldName <> gridHI.Column.FieldName Then
is_changed = True
Dim rowHandle As Integer = lastMouseDownMoveCell.RowHandle
Dim gridCol As GridColumn = lastMouseDownMoveCell.Column
lastMouseDownMoveCell = New GridCell(gridHI.RowHandle, gridHI.Column)
TryCast(view, GridView).InvalidateRowCell(rowHandle, gridCol)
End If
If is_changed Then
TryCast(view, GridView).InvalidateRowCell(lastMouseDownMoveCell.RowHandle, lastMouseDownMoveCell.Column)
End If
ElseIf gridHI.HitTest = GridHitTest.RowEdge AndAlso sourceGridCell Is Nothing Then
baseHI = view.CalcHitInfo(New Point(e.X, e.Y - 3))
gridHI = TryCast(baseHI, GridHitInfo)
If gridHI.HitTest = GridHitTest.RowCell Then
If gridHI.RowHandle = (TryCast(view, GridView)).FocusedRowHandle AndAlso gridHI.Column Is (TryCast(view, GridView)).FocusedColumn Then
TryCast(view, GridView).GridControl.Cursor = Cursors.SizeAll
End If
End If
Else
TryCast(view, GridView).GridControl.Cursor = Cursors.Default
End If
End Sub
Private Sub gridView_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
If sourceGridCell IsNot Nothing AndAlso lastMouseDownMoveCell IsNot Nothing Then
Dim view As BaseView = gridView.GridControl.GetViewAt(e.Location)
Dim val As Object = (TryCast(view, GridView)).GetRowCellValue(sourceGridCell.RowHandle, sourceGridCell.Column)
TryCast(view, GridView).SetRowCellValue(sourceGridCell.RowHandle, sourceGridCell.Column, "")
TryCast(view, GridView).SetRowCellValue(lastMouseDownMoveCell.RowHandle, lastMouseDownMoveCell.Column, val)
TryCast(view, GridView).ClearSelection()
TryCast(view, GridView).SelectCell(lastMouseDownMoveCell.RowHandle, lastMouseDownMoveCell.Column)
TryCast(view, GridView).FocusedRowHandle = lastMouseDownMoveCell.RowHandle
TryCast(view, GridView).FocusedColumn = lastMouseDownMoveCell.Column
sourceGridCell = Nothing
Dim rowHandle As Integer = lastMouseDownMoveCell.RowHandle
Dim gridCol As GridColumn = lastMouseDownMoveCell.Column
lastMouseDownMoveCell = Nothing
TryCast(view, GridView).InvalidateRowCell(rowHandle, gridCol)
End If
End Sub
End Class
End Namespace