wpf-119262-controls-and-libraries-data-grid-drag-and-drop-process-drag-and-drop-drag-and-drop-between-applications.md
The GridControl allows users to drag and drop records between applications.
The following image demonstrates a drag-and-drop operation between Grid controls in separate applications:
Follow the steps below to implement drag-and-drop between Grid controls in individual applications:
Add a GridControl to each application and set the AllowDragDrop property to true to enable drag-and-drop operations, as demonstrated in the following code snippets:
<dxg:GridControl Name="gridControl1">
<!--...-->
<dxg:GridControl.View>
<dxg:TableView AllowDragDrop="True" />
</dxg:GridControl.View>
</dxg:GridControl>
<dxg:GridControl Name="gridControl2">
<!--...-->
<dxg:GridControl.View>
<dxg:TableView AllowDragDrop="True" />
</dxg:GridControl.View>
</dxg:GridControl>
When the AllowDragDrop property is enabled, users can drag and drop records within the same application. To force the second application to accept dropped records, handle the DragRecordOver event as follows:
Check if dragged data exists.
Set the DragEventArgsBase.Effects property to DragDropEffects.Move to allow users to drop records in the second application.
<dxg:GridControl Name="gridControl2">
<!--...-->
<dxg:GridControl.View>
<dxg:TableView AllowDragDrop="True"
DragRecordOver="OnDragRecordOver" />
</dxg:GridControl.View>
</dxg:GridControl>
void OnDragRecordOver(object sender, DragRecordOverEventArgs e) {
if(!e.IsFromOutside)
return;
if(!e.Data.GetDataPresent(typeof(Item)))
return;
e.Effects = DragDropEffects.Move;
e.Handled = true;
}
To prepare a record drag operation, you need to serialize target records in the first application to a JSON string within a StartRecordDrag event handler. Call the SetData() method to add serialized data to a DataObject as demonstrated in the following code snippet:
<dxg:GridControl Name="gridControl1">
<!--...-->
<dxg:GridControl.View>
<dxg:TableView AllowDragDrop="True"
StartRecordDrag="OnStartRecordDrag" />
</dxg:GridControl.View>
</dxg:GridControl>
void OnStartRecordDrag(object sender, StartRecordDragEventArgs e) {
var json = JsonSerializer.Serialize(e.Records);
e.Data.SetData(typeof(Item), json);
}
Handle the DropRecord event in the second application to accept dropped records. An event handler should check if the dropped object contains a string in the JSON format and deserialize the string into record objects.
<dxg:GridControl Name="gridControl2">
<!--...-->
<dxg:GridControl.View>
<dxg:TableView AllowDragDrop="True"
DragRecordOver="OnDragRecordOver"
DropRecord="OnDropRecord" />
</dxg:GridControl.View>
</dxg:GridControl>
void OnDropRecord(object sender, DropRecordEventArgs e) {
if(!e.Data.GetDataPresent(typeof(Item)))
return;
var json = e.Data.GetData(typeof(Item)) as string;
var items = JsonSerializer.Deserialize<Item[]>(json);
DataObject dataObject = new DataObject();
dataObject.SetData(new RecordDragDropData(items.ToArray()));
e.Data = dataObject;
}
Starting with .NET 9, Microsoft no longer includes the implementation of BinaryFormatter in its runtime (due to security risks). We recommend that you use the JsonSerializer to implement drag-and-drop between applications.
If you need to utilize legacy BinaryFormatter, install the BinaryFormatter compatibility package and set the DeserializationSettings.EnableDataObjectBinarySerialization property to true.
Refer to the following Breaking Change for additional information:
Component functionality changes due to BinaryFormatter deprecation
The GridControl allows users to drag and drop records between grid-powered and third-party applications.
To implement this functionality, follow the steps described above. In addition, you need to handle the DropRecord event to convert dragged items into the data format of the GridControl data source.
Follow the steps described above. In addition, handle the StartRecordDrag event to convert DataObject content into a format compatible with the target application.
The following example creates a string comprised of all focused row values delimited by tab characters (Excel-compatible):
void OnTableViewStartRecordDrag(object sender, StartRecordDragEventArgs e) {
var view = (TableView)e.Source;
var values = view.VisibleColumns.Select(column => view.Grid.GetCellDisplayText(view.FocusedRowHandle, column));
e.Data.SetData(DataFormats.StringFormat, string.Join("\t", values));
}
See Also
Drag-and-Drop Between GridControl and ListBoxEdit