Back to Devexpress

DragDropEvents.DragDrop Event

windowsforms-devexpress-dot-utils-dot-dragdrop-dot-dragdropevents-3c03d87d.md

latest20.7 KB
Original Source

DragDropEvents.DragDrop Event

Occurs when a data element is dropped on the control.

Namespace : DevExpress.Utils.DragDrop

Assembly : DevExpress.Utils.v25.2.dll

NuGet Packages : DevExpress.Utils, DevExpress.Wpf.Core

Declaration

csharp
[DXCategory("DragDrop")]
public event DragDropEventHandler DragDrop
vb
<DXCategory("DragDrop")>
Public Event DragDrop As DragDropEventHandler

Event Data

The DragDrop event's data class is DragDropEventArgs. The following properties provide information specific to this event:

PropertyDescription
ActionGets or sets the drag-and-drop action (Copy, Move, etc.) to perform. Inherited from DXDragEventArgs.
CursorGets or sets the mouse pointer. Inherited from DXDragEventArgs.
DataGets or sets the data elements being dragged. Inherited from DXDragEventArgs.
HandledGets or sets whether the event was handled and allows you to suppress the default action. Inherited from DXDefaultEventArgs.
InsertTypeGets or sets whether dragged data elements are inserted before or after a data element under the mouse pointer, or as a child (for tree list only). Inherited from DragOverEventArgsBase.
KeyStateGets the pressed mouse buttons (left, middle, right) and modifier keys (Shift, Ctrl, Alt). Inherited from DXDragEventArgs.
LocationGets the mouse cursor’s position. Inherited from DXDragEventArgs.
SourceGets the source control. Inherited from DXDragEventArgs.
TagGets or sets custom data associated with the drag-and-drop operation. Inherited from DragOverEventArgsBase.
TargetGets the target control. Inherited from DXDragEventArgs.

The event data class exposes the following methods:

MethodDescription
Default()Invokes the default action the attached control performs on the current drag-and-drop operation stage. Inherited from DXDefaultEventArgs.
GetData<T>()Returns the data elements being dragged. Inherited from DXDragEventArgs.

Examples

The example below shows how to copy data from a tree list to a grid.

csharp
using DevExpress.Utils.DragDrop;

//The code below assumes that you are moving data from a tree list to a grid.
//Create new grid rows, and populate them with data from tree list nodes.
private void dragDropEvents1_DragDrop(object sender, DragDropEventArgs e) {
    List<TreeListNode> list = e.Data as List<TreeListNode>;
    foreach (TreeListNode node in list) {
        gridView1.AddNewRow();
        gridView1.SetRowCellValue(GridControl.NewItemRowHandle, gridView1.Columns["DEPARTMENT"], node.GetValue(colDEPARTMENT1));
    }
    e.Handled = true;
}
vb
Imports DevExpress.Utils.DragDrop

'The code below assumes that you are moving data from a tree list to a grid.
'Create new grid rows, and populate them with data from tree list nodes.
Private Sub dragDropEvents1_DragDrop(ByVal sender As Object, ByVal e As DevExpress.Utils.DragDrop.DragDropEventArgs) _
    Handles dragDropEvents1.DragDrop
    Dim list As List(Of TreeListNode) = TryCast(e.Data, List(Of TreeListNode))
    For Each node As TreeListNode In list
        gridView1.AddNewRow()
        gridView1.SetRowCellValue(GridControl.NewItemRowHandle, gridView1.Columns("DEPARTMENT"), node.GetValue(colDEPARTMENT1))
    Next node
    e.Handled = True
End Sub

Note

When you handle the DragDrop event for a grid view, use the static (Shared in VB) DragDropGridEventArgs.GetDragDropGridEventArgs method to calculate arguments specific to the grid view.

csharp
using DevExpress.Utils.DragDrop;
using DevExpress.XtraGrid.Views.Grid;

dragDropEvents1.DragDrop += Behavior_DragDrop;

private void Behavior_DragDrop(object sender, DragDropEventArgs e) {
   DragDropGridEventArgs args = DragDropGridEventArgs.GetDragDropGridEventArgs(e);
   //You can also cast DragDropEventArgs to DragDropGridEventArgs.
   //DragDropGridEventArgs args = (DragDropGridEventArgs)e;
}
vb
Imports DevExpress.Utils.DragDrop
Imports DevExpress.XtraGrid.Views.Grid

AddHandler DragDropEvents1.DragDrop, AddressOf Behavior_DragDrop
Private Sub Behavior_DragDrop(ByVal sender As Object, ByVal e As DragDropEventArgs)
   Dim args As DragDropGridEventArgs = DragDropGridEventArgs.GetDragDropGridEventArgs(e)
   'You can also cast DragDropEventArgs to DragDropGridEventArgs.
   'Dim args As DragDropGridEventArgs = CType(e, DragDropGridEventArgs)
End Sub

Note

Run the XtraTreeList or XtraGrid demo and click Open Solution for more examples.

To allow users to move child rows between detail views in the GridControl, do the following:

  • Attach the Behavior to the master view in the Visual Studio Designer or in code.
  • Use the ViewRegistered event to attach the Behavior to the detail view. To detach the Behavior, use the ViewRemoved event.
  • Handle the DragDrop event of the Behavior attached to the master view to move the processed child row from the source detail view to the target detail view.

In this example, the Behavior Manager is placed on the component tray and the Drag-and-Drop Behavior is attached to the main view in the Visual Studio Designer.

csharp
using DevExpress.Utils.DragDrop;
using DevExpress.XtraGrid.Views.Grid;

gridView1.OptionsBehavior.Editable = false;
gridView1.OptionsSelection.MultiSelect = true;
gridControl1.DataSource = CreateDataTable();
gridControl1.ViewRegistered += GridControl1_ViewRegistered;

private void dragDropEvents1_DragDrop(object sender, DragDropEventArgs e) {
    GridView masterView = e.Source as GridView;
    // Cast the event arguments to the DragDropGridEventArgs type
    // or call the static (Shared in VB) DragDropGridEventArgs.GetDragDropGridEventArgs method
    // to get grid-specific event arguments.
    DragDropGridEventArgs realArgs = (DragDropGridEventArgs)e;
    GridView sourceView = realArgs.Source as GridView;
    GridView targetView = realArgs.Target as GridView;

    var view1 = gridControl1.GetViewAt(gridControl1.PointToClient(e.Location));
    if(sourceView != null && targetView != null) {
        // Get the processed child row's parent ID.
        var newParentId = masterView.GetRowCellValue(targetView.SourceRowHandle, "Id");
        foreach(DataRowView dataRow in realArgs.DataRows) {
            // Update the processed child row's parent ID.
            dataRow.Row["ParentId"] = newParentId;
        }
        e.Handled = true;
    }
}

private void GridControl1_ViewRegistered(object sender, DevExpress.XtraGrid.ViewOperationEventArgs e) {
    if(e.View.IsDetailView) {
        // It is assumed that the Behavior Manager is placed
        // to the component tray in the Visual Studio Designer.
        behaviorManager1.Attach<DragDropBehavior>(e.View);
    }
}

public DataTable CreateDataTable() {
    masterTable = new DataTable();
    masterTable.Columns.Add("Id", typeof(int));
    masterTable.Columns.Add("Name");
    masterTable.Columns.Add("IsActive", typeof(bool));
    masterTable.Columns.Add("OrderCount", typeof(int));
    masterTable.Columns.Add("RegistrationDate", typeof(DateTime));

    for(int i = 0; i < 10; i++) {
        masterTable.Rows.Add(i, "Name" + i, i % 2 == 0, i * 10, DateTime.Now.AddDays(i));
    }

    DataTable childTable = new DataTable();
    childTable.Columns.Add("ParentId", typeof(int));
    childTable.Columns.Add("Id", typeof(int));
    childTable.Columns.Add("Name");

    for(int i = 0; i < 20; i++) {
        childTable.Rows.Add(i % 10, i, "Name" + i);
    }

    DataSet set = new DataSet();
    set.Tables.Add(masterTable);
    set.Tables.Add(childTable);
    set.Relations.Add(masterTable.Columns["Id"], childTable.Columns["ParentId"]);

    return masterTable;
}
vb
Imports DevExpress.Utils.DragDrop
Imports DevExpress.XtraGrid.Views.Grid

gridView1.OptionsBehavior.Editable = False
gridView1.OptionsSelection.MultiSelect = True
gridControl1.DataSource = CreateDataTable()
AddHandler gridControl1.ViewRegistered, AddressOf GridControl1_ViewRegistered

Private Sub dragDropEvents1_DragDrop(ByVal sender As Object, ByVal e As DragDropEventArgs) Handles dragDropEvents1.DragDrop
    ' Cast the event arguments to the DragDropGridEventArgs type
    ' or call the static (Shared in VB) DragDropGridEventArgs.GetDragDropGridEventArgs method
    ' to get grid-specific event arguments.
    Dim masterView As GridView = TryCast(e.Source, GridView)
    Dim realArgs As DragDropGridEventArgs = CType(e, DragDropGridEventArgs)
    Dim sourceView As GridView = TryCast(realArgs.Source, GridView)
    Dim targetView As GridView = TryCast(realArgs.Target, GridView)

    Dim view1 = gridControl1.GetViewAt(gridControl1.PointToClient(e.Location))
    If sourceView IsNot Nothing AndAlso targetView IsNot Nothing Then
        ' Get the processed child row's parent ID.
        Dim newParentId = masterView.GetRowCellValue(targetView.SourceRowHandle, "Id")
        For Each dataRow As DataRowView In realArgs.DataRows
            ' Update the processed child row's parent ID.
            dataRow.Row("ParentId") = newParentId
        Next dataRow
        e.Handled = True
    End If
End Sub

Private Sub GridControl1_ViewRegistered(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.ViewOperationEventArgs)
    If e.View.IsDetailView Then
        ' It is assumed that the Behavior Manager is placed
        ' to the component tray in the Visual Studio Designer.
        behaviorManager1.Attach(Of DragDropBehavior)(e.View)
    End If
End Sub

Public Function CreateDataTable() As DataTable
    masterTable = New DataTable()
    masterTable.Columns.Add("Id", GetType(Integer))
    masterTable.Columns.Add("Name")
    masterTable.Columns.Add("IsActive", GetType(Boolean))
    masterTable.Columns.Add("OrderCount", GetType(Integer))
    masterTable.Columns.Add("RegistrationDate", GetType(Date))
    For i As Integer = 0 To 9
        masterTable.Rows.Add(i, "Name" & i, i Mod 2 = 0, i * 10, Date.Now.AddDays(i))
    Next i

    Dim childTable As New DataTable()
    childTable.Columns.Add("ParentId", GetType(Integer))
    childTable.Columns.Add("Id", GetType(Integer))
    childTable.Columns.Add("Name")

    For i As Integer = 0 To 19
        childTable.Rows.Add(i Mod 10, i, "Name" & i)
    Next i

    Dim dataSet As New DataSet()
    dataSet.Tables.Add(masterTable)
    dataSet.Tables.Add(childTable)
    dataSet.Relations.Add(masterTable.Columns("Id"), childTable.Columns("ParentId"))

    Return masterTable
End Function

If the application uses business objects (for example, Persistent Objects), these objects must have a default constructor to allow the Drag-and-Drop Behavior to move/copy data items between controls.

In the example below, business objects are stored in an XPCollection. To load and save the business objects, the collection uses a Session that is also passed to every business object in its constructor. If a business object does not have a default constructor, you must handle the target control’s DragOver and DragDrop events to manually move/copy the dragged item.

The DragOver event handler sets the Action event argument to Move to allow users to drop data items to the target control. The DragDrop event handler manually moves the dragged item from the source to the target control.

csharp
using DevExpress.Data.Filtering;
using DevExpress.Utils.DragDrop;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

private UnitOfWork treeListSession = new UnitOfWork();
private UnitOfWork gridSession = new UnitOfWork();

XpoDefault.DataLayer = new SimpleDataLayer(new InMemoryDataStore());
XpoDefault.Session = null;
if (Convert.ToInt32(treeListSession.Evaluate<Customer>(new AggregateOperand(null, null, Aggregate.Count, null), null)) == 0) {
    for (int i = 0; i <= 9; i++)
        new Customer(treeListSession) { Name = string.Format("Name {0}", i) };
    treeListSession.CommitChanges();
}
if (Convert.ToInt32(gridSession.Evaluate<Customer>(new AggregateOperand(null, null, Aggregate.Count, null), null)) == 0) {
    for (int i = 0; i <= 9; i++)
        new Customer(treeListSession) { Name = string.Format("Name {0}", i) };
    gridSession.CommitChanges();
}

XPClassInfo treeClassInfo = treeListSession.Dictionary.GetClassInfo(typeof(Customer));
treeList1.DataSource = new XPCollection(treeListSession, treeClassInfo);            
XPClassInfo gridClassInfo = gridSession.Dictionary.GetClassInfo(typeof(Customer));
gridControl1.DataSource = new XPCollection(gridSession, gridClassInfo);

private void dragDropEvents1_DragOver(object sender, DragOverEventArgs e) {
    if (object.ReferenceEquals(e.Source, e.Target))
        return;
    e.Default();
    e.Action = DragDropActions.Move;
    e.Cursor = Cursors.Arrow;
}

private void dragDropEvents1_DragDrop(object sender, DragDropEventArgs e) {
    GridView view = e.Source as GridView;
    TreeList treeList = e.Target as TreeList;
    if (view != null && treeList != null) {
        int[] rowHandles = e.Data as int[];
        Customer customer = view.GetRow(rowHandles[0]) as Customer;

        XPCollection xpCollection = treeList.DataSource as XPCollection;

        Customer cloneCustomer = new Customer(treeListSession);
        cloneCustomer.Name = customer.Name;
        customer.Delete();
        treeListSession.CommitChanges();
        gridSession.CommitChanges();
        xpCollection.Add(cloneCustomer);

        TreeListHitInfo hitInfo = treeList.CalcHitInfo(treeList.PointToClient(e.Location));
        TreeListNode targetNode = hitInfo.Node;
        TreeListNode newNode = treeList.FindNodeByFieldValue("Oid", cloneCustomer.Oid);

        switch (e.InsertType) {
            case InsertType.None:
            case InsertType.AsChild:
            case InsertType.After:
            case InsertType.Before:
                treeList.MoveNode(newNode, newNode.ParentNode, false, treeList.Nodes.IndexOf(targetNode));
                break;
        }
    }
}

class Customer : XPObject {
    [Size(SizeAttribute.DefaultStringMappingFieldSize)]
    public string Name {
        get {
            return name;
        }
        set {
            SetPropertyValue("Name", ref name, value);
        }
    }

    private string name;

    public Customer(Session session) : base(session) {
    }
}
vb
Imports DevExpress.Data.Filtering
Imports DevExpress.Utils.DragDrop
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Imports DevExpress.Xpo.Metadata
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Private treeListSession As New UnitOfWork()
Private gridSession As New UnitOfWork()

XpoDefault.DataLayer = New SimpleDataLayer(New InMemoryDataStore())
XpoDefault.Session = Nothing
If Convert.ToInt32(treeListSession.Evaluate(Of Customer)(New AggregateOperand(Nothing, Nothing, Aggregate.Count, Nothing), Nothing)) = 0 Then
    For i As Integer = 0 To 9
        Dim tempVar = New Customer(treeListSession) With {.Name = String.Format("Name {0}", i)}
    Next i
    treeListSession.CommitChanges()
End If
If Convert.ToInt32(gridSession.Evaluate(Of Customer)(New AggregateOperand(Nothing, Nothing, Aggregate.Count, Nothing), Nothing)) = 0 Then
    For i As Integer = 0 To 9
        Dim tempVar = New Customer(treeListSession) With {.Name = String.Format("Name {0}", i)}
    Next i
    gridSession.CommitChanges()
End If

Dim treeClassInfo As XPClassInfo = treeListSession.Dictionary.GetClassInfo(GetType(Customer))
treeList1.DataSource = New XPCollection(treeListSession, treeClassInfo)
Dim gridClassInfo As XPClassInfo = gridSession.Dictionary.GetClassInfo(GetType(Customer))
gridControl1.DataSource = New XPCollection(gridSession, gridClassInfo)

Private Sub dragDropEvents1_DragOver(ByVal sender As Object, ByVal e As DragOverEventArgs) Handles dragDropEvents1.DragOver
    If Object.ReferenceEquals(e.Source, e.Target) Then
        Return
    End If
    e.Default()
    e.Action = DragDropActions.Move
    e.Cursor = Cursors.Arrow
End Sub

Private Sub dragDropEvents1_DragDrop(ByVal sender As Object, ByVal e As DragDropEventArgs) Handles dragDropEvents1.DragDrop
    Dim view As GridView = TryCast(e.Source, GridView)
    Dim treeList As TreeList = TryCast(e.Target, TreeList)
    If view IsNot Nothing AndAlso treeList IsNot Nothing Then
        Dim rowHandles() As Integer = TryCast(e.Data, Integer())
        Dim customer As Customer = TryCast(view.GetRow(rowHandles(0)), Customer)

        Dim xpCollection As XPCollection = TryCast(treeList.DataSource, XPCollection)

        Dim cloneCustomer As New Customer(treeListSession)
        cloneCustomer.Name = customer.Name
        customer.Delete()
        treeListSession.CommitChanges()
        gridSession.CommitChanges()
        xpCollection.Add(cloneCustomer)

        Dim hitInfo As TreeListHitInfo = treeList.CalcHitInfo(treeList.PointToClient(e.Location))
        Dim targetNode As TreeListNode = hitInfo.Node
        Dim newNode As TreeListNode = treeList.FindNodeByFieldValue("Oid", cloneCustomer.Oid)

        Select Case e.InsertType
            Case InsertType.None, InsertType.AsChild, InsertType.After, InsertType.Before
                treeList.MoveNode(newNode, newNode.ParentNode, False, treeList.Nodes.IndexOf(targetNode))
        End Select
    End If
End Sub

Public Class Customer
    Inherits XPObject

    <Size(SizeAttribute.DefaultStringMappingFieldSize)>
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Name", _Name, value)
        End Set
    End Property

    Private _Name As String

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
End Class

See Also

DragDropEvents Class

DragDropEvents Members

DevExpress.Utils.DragDrop Namespace