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
| Public Class FDragDrop
Private Sub Form1_DoubleClick(sender As Object, e As EventArgs) Handles Me.DoubleClick
Dim F2 As New FDragDrop
F2.Show()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Pour les tests
DGV1.Columns.Add("Col1", "Col.1")
DGV1.Columns.Add("Col2", "Col.2")
DGV1.Rows.Add("1", "az")
DGV1.Rows.Add("2", "aze")
DGV1.Rows.Add("3", "azer")
DGV1.Rows.Add("4", "azert")
DGV1.Rows.Add("5", "azerty")
DGV2.Columns.Add("Col1", "Col.1")
DGV2.Columns.Add("Col2", "Col.2")
' Réglages nécessaires
DGV1.AllowDrop = True
DGV1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DGV2.AllowDrop = True
DGV2.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Dim SelectionFinie As Boolean
' Sélectionner et démarrer le DragDrop à partir de DGV1
Private Sub DGV1_MouseDown(sender As Object, e As MouseEventArgs) Handles DGV1.MouseDown
If (e.Button = MouseButtons.Left) Then
If SelectionFinie Then
DGV1.DoDragDrop(DGV1.SelectedRows, DragDropEffects.Copy)
SelectionFinie = False
Else
DGV1.ClearSelection()
End If
Else
SelectionFinie = False
End If
End Sub
' Sélectionner et démarrer le DragDrop à partir de DGV2
Private Sub DGV2_MouseDown(sender As Object, e As MouseEventArgs) Handles DGV2.MouseDown
If (e.Button = MouseButtons.Left) Then
If SelectionFinie Then '
DGV2.DoDragDrop(DGV2.SelectedRows, DragDropEffects.Copy)
SelectionFinie = False
Else
DGV2.ClearSelection()
End If
Else
SelectionFinie = False
End If
End Sub
' Fin de sélection sur DGV1
Private Sub DGV1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DGV1.MouseUp
If (DGV1.SelectedRows.Count > 0) Then
SelectionFinie = True
End If
End Sub
' Fin de sélection sur DGV2
Private Sub DGV2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DGV2.MouseUp
If (DGV2.SelectedRows.Count > 0) Then
SelectionFinie = True
End If
End Sub
' Définir le type de Drag-Drop au début du processus lancé de DGV1
Private Sub DGV1_DragEnter(sender As Object, e As DragEventArgs) Handles DGV1.DragEnter
If (e.Data Is Nothing) Then
e.Effect = DragDropEffects.None
Else
e.Effect = DragDropEffects.Copy
End If
End Sub
' Définir le type de Drag-Drop au début du processus lancé de DGV2
Private Sub DGV2_DragEnter(sender As Object, e As DragEventArgs) Handles DGV2.DragEnter
If (e.Data Is Nothing) Then
e.Effect = DragDropEffects.None
Else
e.Effect = DragDropEffects.Copy
End If
End Sub
' Déposer dans DGV1 les données capturées par le processus Drag-Drop
Private Sub DGV1_DragDrop(sender As Object, e As DragEventArgs) Handles DGV1.DragDrop
If (e.Data IsNot Nothing) Then
Dim Selection As DataGridViewSelectedRowCollection = CType(e.Data.GetData(GetType(DataGridViewSelectedRowCollection)), DataGridViewSelectedRowCollection)
Dim NCol As Integer = Selection(0).Cells.Count
For Each R As DataGridViewRow In Selection
Dim NR As DataGridViewRow = CType(R.Clone(), DataGridViewRow)
For C As Integer = 0 To NCol - 1
NR.Cells(C).Value = R.Cells(C).Value
Next
DGV1.Rows.Add(NR)
Next
End If
DGV1.ClearSelection()
End Sub
' Déposer dans DGV2 les données capturées par le processus Drag-Drop
Private Sub DGV2_DragDrop(sender As Object, e As DragEventArgs) Handles DGV2.DragDrop
If (e.Data IsNot Nothing) Then
Dim Selection As DataGridViewSelectedRowCollection = CType(e.Data.GetData(GetType(DataGridViewSelectedRowCollection)), DataGridViewSelectedRowCollection)
Dim NCol As Integer = Selection(0).Cells.Count
For Each R As DataGridViewRow In Selection
Dim NR As DataGridViewRow = CType(R.Clone(), DataGridViewRow)
For C As Integer = 0 To NCol - 1
NR.Cells(C).Value = R.Cells(C).Value
Next
DGV2.Rows.Add(NR)
Next
End If
DGV2.ClearSelection()
End Sub
End Class |
Partager