Skip to main content

Drag-and-Drop Between Applications

  • 5 minutes to read

The GridControl allows dragging records and dropping them in external applications. This topic demonstrates how to implement this behavior.

To Implement Drag-and-Drop Between Applications

The following image shows 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. Now you can drag records from the first application but not drop them in the second one. The following steps to allow dropping:

    <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:

  • A basic data type (such as string, int, etc.) or
  • An object that implements the ISerializable interface which allows .NET to transfer your object into a stream of bytes and reconstruct the object in another application domain.
  1. Apply the SerializableAttribute attribute 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 that is 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 that is 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;
       }
    }
    

To 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.

To 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 the DataViewBase.StartRecordDrag event handler, you should cutsomize DataObject to include data in the format the target application supports.

The code sample below shows how to prepare data for drag-and-drop to Excel. In this case, you should specify a string with the focused row‘s cell values separated by tabulation symbols:

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