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
Handle the DataViewBase.StartRecordDrag event.
<dxg:TreeListView AllowDragDrop="True" StartRecordDrag="OnStartRecordDrag" />
Define records that cannot be dragged.
Set the StartRecordDragEventArgs.AllowDrag property to false to prevent records from being dragged.
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
Handle the DataViewBase.DragRecordOver event.
<dxg:TreeListView AllowDragDrop="True" DragRecordOver="OnDragRecordOver" />
Define records that reject the data.
Set the Effects property to DragDropEffects.None to make the drop target reject the data.
Set the Handled property to true.
#To Customize Dragged Items
Handle the DataViewBase.DropRecord event.
<dxg:TreeListView AllowDragDrop="True" DropRecord="OnDropRecord" />
Use the DataObject.GetData method to extract data.
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 = ""; } } }
#To Prevent Removing Items After Dropping
Handle the DataViewBase.CompleteRecordDragDrop event.
<dxg:TableView AllowDragDrop="True" CompleteRecordDragDrop="OnCompleteRecordDragDrop" />
Set the Handled property to 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:
- Drag-and-Drop Between GridControl and ListBoxEdit
- Drag-and-Drop Between GridControls
- Drag-and-Drop Between GridControl and Other Controls
- Drag-and-Drop Between Applications