Skip to main content
A newer version of this page is available. .

Drag And Drop Behavior

  • 4 minutes to read

The Drag and Drop Behavior allows you to add support for drag-and-drop operations between supported controls.

DragAndDropBehavior_Grid

Supported controls

Behavior options

  • Target—specifies the control to which the behavior is attached;
  • PreviewVisible—specifies whether a preview of item(s) being dragged is displayed during operation;
  • InsertIndicatorVisible—specifies whether the indicator is showing items’ insertion position;
  • AllowDrop—specifies whether the attached control allows an end-user to drop the item(s) on it.

The code below shows how to assign a Drag and Drop Behavior to a control using the BehaviorManager.Attach method and specify its options.


using DevExpress.Utils.DragDrop;

behaviorManager1.Attach<DragDropBehavior>(gridView1, behavior => {
    behavior.Properties.AllowDrop = true;
    behavior.Properties.InsertIndicatorVisible = true;
    behavior.Properties.PreviewVisible = true;
});

behaviorManager1.Attach<DragDropBehavior>(gridView2, behavior => {
    behavior.Properties.AllowDrop = false;
    behavior.Properties.InsertIndicatorVisible = false;
    behavior.Properties.PreviewVisible = false;
});

Note

Drag and Drop Behaviors should be attached to the source and target controls to work correctly. Disable the source control’s AllowDrop setting to allow data to be transferred one way only.

Note

For a grid control with master-detail data presentation, attach a Drag and Drop Behavior to the main view (see GridControl.MainView).

Important

Business objects must have a default constructor with no parameters if the behavior is attached to data-aware controls that store these objects defined in code. Otherwise, rows that display these objects cannot be moved between controls.

Behavior events

  • BeginDragDrop—occurs when a drag-and-drop operation starts allowing you to cancel the operation, or provide a custom preview image;
  • DragEnter—occurs when an object is dragged into the control’s bounds;
  • DragOver—occurs when an object is dragged over the control’s bounds;
  • DragLeave—occurs when an object is dragged out of the control’s bounds;
  • DragDrop—occurs when a drag-and-drop operation is completed;
  • EndDragDrop—allows you to perform custom actions after a drag-and-drop operation is completed.

A DragDropEvents component is automatically added to your form when you attach a Drag and Drop Behavior to a control at design time. You can use this component to subscribe to the behavior’s events.


dragDropEvents1.DragDrop += dragDropEvents1_DragDrop;
dragDropEvents1.DragOver += dragDropEvents1_DragOver;

When handling the drag and drop events for a grid view, use the following static (Shared in VB) methods to calculate GridView-specific arguments:

  • DragOverGridEventArgs.GetDragOverGridEventArgs—for the DragOver event;
  • DragDropGridEventArgs.GetDragDropGridEventArgs—for the DragDrop event.

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

dragDropEvents1.DragOver += Behavior_DragOver;
private void Behavior_DragOver(object sender, DragOverEventArgs e) {
    DragOverGridEventArgs args = DragOverGridEventArgs.GetDragOverGridEventArgs(e);
    //You can also cast DragOverEventArgs to DragOverGridEventArgs.
    //DragOverGridEventArgs args = (DragOverGridEventArgs)e;
}

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;
}

When handling the drag-and-drop events for a tree list, use the Default method to calculate TreeList-specific arguments.


dragDropEvents1.DragOver += DragDropEvents1_DragOver;

private void DragDropEvents1_DragOver(object sender, DevExpress.Utils.DragDrop.DragOverEventArgs e) {
    e.Default();
    if (e.InsertType != DevExpress.Utils.DragDrop.InsertType.AsChild){
        e.Action = DevExpress.Utils.DragDrop.DragDropActions.None;
        e.InsertType = DevExpress.Utils.DragDrop.InsertType.None;
    }
    else {
        e.Action = DevExpress.Utils.DragDrop.DragDropActions.All;
    }
    e.Handled = true;
}

Note

The Handled event argument should be set to true for event handlers to be in effect.