Skip to main content

DevExpress v25.1 Update — Your Feedback Matters

Our What's New in v25.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Drag-and-Drop Between Applications

  • 3 minutes to read

The GridControl allows users to drag and drop records between applications.

#Drag-and-Drop Operations Between Grid-Powered Applications

The following image demonstrates a drag-and-drop operation between Grid controls in separate applications:

DragDropBetweenApplications

Follow the steps below to implement drag-and-drop between Grid controls in individual applications:

#Step 1. Create Controls

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:

#First Application

<dxg:GridControl Name="gridControl1">
    <!--...-->
    <dxg:GridControl.View>
        <dxg:TableView AllowDragDrop="True" />
    </dxg:GridControl.View>
</dxg:GridControl>

#Second Application

<dxg:GridControl Name="gridControl2">
    <!--...-->
    <dxg:GridControl.View>
        <dxg:TableView AllowDragDrop="True" />
    </dxg:GridControl.View>
</dxg:GridControl>

#Step 2. Handle the DragRecordOver Event

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

#Step 3. Handle the StartRecordDrag Event and Serialize Data

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

#Step 4. Handle the DropRecord Event and Deserialize Data

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

#Migration from BinaryFormatter in .NET 9+

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

#Drag-and-Drop Operations Between Grid-Powered and Third-Party Applications

The GridControl allows users to drag and drop records between grid-powered and third-party applications.

#Drag-and-Drop from External Applications to Grid

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.

#Drag-and-Drop from Grid to External Applications

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