Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 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

  • 5 minutes to read

The GridControl allows users to drag and drop records between applications based on .NET 8 and earlier .NET versions.

Note

If the project is built for .NET 9, you can drag and drop records only within the same application. To drag and drop records between applications, 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.

#Implement Drag-and-Drop Between Applications

The following image demonstrates a drag-and-drop operation between GridControls in different applications:

DragDropBetweenApplications

#Steps 1-2. Add Controls

  1. Add the GridControl to the first application:

    <dxg:GridControl Name="gridControl1">
       <!--...-->
       <dxg:GridControl.View>
          <dxg:TableView />
       </dxg:GridControl.View>
    </dxg:GridControl>
    
  2. Add the GridControl to the second application:

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

#Steps 3-5. Allow Drag-and-Drop

  1. Set the DataViewBase.AllowDragDrop property to true to enable drag-and-drop in the GridControl from the first application:

    <dxg:GridControl Name="gridControl1">
        <!--...-->
        <dxg:GridControl.View>
            <dxg:TableView AllowDragDrop="True" />
        </dxg:GridControl.View>
    </dxg:GridControl>
    
  2. Set the DataViewBase.AllowDragDrop property to true to enable drag-and-drop in the GridControl from the second application:

    <dxg:GridControl Name="gridControl2">
        <!--...-->
        <dxg:GridControl.View>
            <dxg:TableView AllowDragDrop="True" />
        </dxg:GridControl.View>
    </dxg:GridControl>
    
  3. You can drag records from the first application but cannot drop them in the second one. The following steps 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.Data.GetDataPresent(typeof(RecordDragDropData))) {
            e.Effects = DragDropEffects.Move;
            e.Handled = true;
        }
    }
    

#Steps 6-8. Transfer Data Between Applications

Note

Drag-and-drop between applications requires one of the following:

  • A basic data type (such as string, int, etc.)
  • An object that implements the ISerializable interface that allows .NET to serialize the object and reconstruct it in another application domain.
  1. Apply the SerializableAttribute to the data type involved in the drag-and-drop operation to indicate that instances of this type can be serialized:

    [Serializable]
    public class Employee {
       // ...   
    }
    
  2. Serialize data transferred from the first application:

    <dxg:GridControl Name="gridControl1">
        <!--...-->
        <dxg:GridControl.View>
            <dxg:TableView AllowDragDrop="True" StartRecordDrag="OnStartRecordDrag" />
        </dxg:GridControl.View>
    </dxg:GridControl>
    
    void OnStartRecordDrag(object sender, StartRecordDragEventArgs e) {
       e.Data.SetData(DataFormats.Serializable, e.Records);
    }
    
  3. Retrieve serialized data transferred to the second application:

    <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(DataFormats.Serializable)) {
            object data = e.Data.GetData(DataFormats.Serializable);
            List<Employee> employees = new List<Employee>();
            foreach (Employee employee in (IList)data) {
                employees.Add(employee);
            }
            RecordDragDropData recordDragDropData = new RecordDragDropData(employees.ToArray());
            DataObject dataObject = new DataObject();
            dataObject.SetData(recordDragDropData);
            e.Data = dataObject;
        }
    }
    

#Implement Drag-and-Drop Objects (for example, files) from an External Application to GridControl

Follow the steps from the tutorial above. In addition to these steps, in the DataViewBase.DropRecord event handler, you should convert dragged items to the target GridControl‘s data source type.

#Implement Drag-and-Drop from GridControl to Other Applications (for example, Excel)

Follow the steps from the tutorial above. In addition to these steps, in a DataViewBase.StartRecordDrag event handler, you should customize DataObject to include data in the format supported by the target application.

The example below prepares data for drag-and-drop to Excel. In this case, you should specify a string with the focused row‘s cell values separated by tab characters:

void OnStartRecordDrag(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