How to drag and drop multiple rows from xtragrid to xtratreelist in VB.NET?
In this article, I will show the drag and drop of multiple rows from Xtragrid to DevExpress Treelist in VB.NET. There is only a small difference between drag and drop of single and multiple rows. The multiselectmode should be made true. Mousedown, MouseMove, DragEnter, DragDrop events are mentioned here.
Drag and drop of multiple rows from DevExpress Datagrid to DevExpress Treelist in VB.NET
In this article, I will show the drag and drop of multiple rows from Xtragrid to Treelist.
'Assign these globally
Private downHitInfo As GridHitInfo = Nothing
Private hitInfo As GridHitInfo = Nothing
Dim dt As DataTable
Here is the code for Mousedown event where the data is selected from the xtragrid by selecting the rows.
Private Sub gridParts_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles gridParts.MouseDown
hitInfo = GridView1.CalcHitInfo(New Point(e.X, e.Y))
If Control.ModifierKeys <> Keys.None Then Exit Sub
If e.Button = MouseButtons.Left AndAlso hitInfo.RowHandle >= 0 Then
downHitInfo = hitInfo
End If
End Sub
Here is the code for MouseMove event.
In this we could get all the rows that is selected from the datagrid. The rowhandle of each row is obtained.
Private Sub gridParts_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles gridParts.MouseMove
If e.Button = MouseButtons.Left And Not downHitInfo Is Nothing Then
Dim dragSize As Size = SystemInformation.DragSize
Dim dragRect As Rectangle = New Rectangle(New Point(downHitInfo.HitPoint.X - dragSize.Width / 2, _
downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize)
If Not dragRect.Contains(New Point(e.X, e.Y)) Then
Dim info As GridViewInfo
info = CType(GridView1.GetViewInfo(), GridViewInfo)
If info.RowsInfo.Count = 1 Then
Else
Dim selectedRows() As Integer = GridView1.GetSelectedRows()
gridParts.DoDragDrop(selectedRows, DragDropEffects.Copy)
downHitInfo = Nothing
Dim ev As DevExpress.Utils.DXMouseEventArgs = DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e)
ev.Handled = True
End If
End If
End If
End Sub
Here is the code for DragEnter event.
What should happen when we select the data by holding the control key or shift key and enter the treelist.
Private Sub treeList1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeList1.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Here is the code for DragDrop event.
In this, we get where the drop is done in the treelist and the selected rows for dropping to the treelist. Using rowhandle, we can convert those rows into datarows. Move those rows into a table.Pass this table and to where it is moved as parameters for the function 'MoveData'. You could write whatever operation you need to perform in this function.
Private Sub treeList1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeList1.DragDrop
dt.Clear()
Dim selectedRows() As Integer = TryCast(e.Data.GetData(GetType(Integer())), Integer())
Dim i As Integer = 0
If selectedRows.Length <> 0 Then
For Each rowHandle As Integer In selectedRows
Dim row As DataRow = GridView1.GetDataRow(rowHandle)
dt.ImportRow(row)
Next rowHandle
End If
Dim hi As TreeListHitInfo = TreeList1.CalcHitInfo(TreeList1.PointToClient(New Point(e.X, e.Y)))
If hi Is Nothing Then
Return
End If
Dim arrlist As ArrayList = TreeList1.GetDataRecordByNode(hi.Node)
Dim str As String = arrlist(0).ToString
MoveData(dt, str)
End Sub
Dont forget to give these two lines in the code
GridView1.OptionsSelection.MultiSelect = True
GridView1.OptionsBehavior.Editable = False
It is given to select multiple rows and to avoid editing. If editing is allowed, mousedown event will be done for editing. Otherwise if editing needs to be done, set
GridView2.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseUp
Hope this helps for someone here.