Skip to main content

Process Drag-and-Drop Operations

  • 3 minutes to read

This topic demonstrates how to customize the GridControl‘s drag-and-drop functionality to support a wide variety of drag-and-drop scenarios.

To Prevent Dragging Specific Records

  1. Handle the DataViewBase.StartRecordDrag event.

    <dxg:TreeListView AllowDragDrop="True" StartRecordDrag="OnStartRecordDrag" /> 
    
  2. Define records that cannot be dragged.

  3. Set the StartRecordDragEventArgs.AllowDrag property to false to prevent records from being dragged.

  4. Set the Handled property to true.

    void OnStartRecordDrag(object sender, StartRecordDragEventArgs e) {
        if (e.Records.Any(x => view.GetNodeByContent(x).HasChildren)) {
            e.AllowDrag = false;
            e.Handled = true;
        }
    } 
    

    The code sample above allows dragging only leaf nodes.

To Prevent Dropping Items Into Specific Records

  1. Handle the DataViewBase.DragRecordOver event.

    <dxg:TreeListView AllowDragDrop="True" DragRecordOver="OnDragRecordOver" />
    
  2. Define records that reject the data.

  3. Set the Effects property to DragDropEffects.None to make the drop target reject the data.

  4. Set the Handled property to true.

    void OnDragRecordOver(object sender, DragRecordOverEventArgs e) {
        string position = ((Employee)e.TargetRecord).Position;
        if (position == "President" || position == "Vice President") {
            e.Effects = DragDropEffects.None;
            e.Handled = true;
        }
    }
    

To Customize Dragged Items

  1. Handle the DataViewBase.DropRecord event.

    <dxg:TreeListView AllowDragDrop="True" DropRecord="OnDropRecord" />
    
  2. Use the DataObject.GetData method to extract data.

  3. Modify extracted data.

    void OnDropRecord(object sender, DropRecordEventArgs e) {
        object data = e.Data.GetData(typeof(RecordDragDropData));
        foreach (Employee employee in ((RecordDragDropData)data).Records) {
            employee.Position = ((Employee)e.TargetRecord).Position;
            employee.Department = ((Employee)e.TargetRecord).Department;
        }
        if (e.DropPosition == DropPosition.Inside) {
            foreach(Employee employee in ((RecordDragDropData)data).Records) {
                employee.Position = "";
            }
        }
    }
    

    View Example: How to manually control Drag-and-Drop in the GridControl

To Prevent Removing Items After Dropping

  1. Handle the DataViewBase.CompleteRecordDragDrop event.

    <dxg:TableView AllowDragDrop="True" CompleteRecordDragDrop="OnCompleteRecordDragDrop" />  
    
  2. Set the Handled property to true.

    void OnCompleteRecordDragDrop(object sender, CompleteRecordDragDropEventArgs e) {
        e.Handled = true;
    } 
    

To Implement Drag-and-Drop Between Controls and Applications

The GridControl allows dragging records and dropping them in external controls that support drag-and-drop functionality. You can perform drag-and-drop operations within a single application, or between different applications. Refer to the following topics for more information:

See Also