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:
Steps 1-2. Add Controls
Add the GridControl to the first application:
<dxg:GridControl Name="gridControl1"> <!----> <dxg:GridControl.View> <dxg:TableView /> </dxg:GridControl.View> </dxg:GridControl>
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
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>
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>
Now you can drag records from the first application but not drop them in the second one. The following steps to allow dropping:
- Handle the DataViewBase.DragRecordOver event on the GridControl from the second application.
- Check whether dragged data is available using the DataObject.GetDataPresent method. The code sample below allows you to drop only the RecordDragDropData data.
- Determine an operation’s type. Set the DragEventArgsBase.Effects property to DragDropEffects.Move to allow dropping.
<dxg:GridControl Name="gridControl2"> <!----> <dxg:GridControl.View> <dxg:TableView AllowDragDrop="True" DragRecordOver="OnDragRecordOver" /> </dxg:GridControl.View> </dxg:GridControl>
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.
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:
Serialize data that is transferred from the first application:
- Handle the DataViewBase.StartRecordDrag event on the GridControl from the first application.
- Add data to a data object in the DataFormats.Serializable data format using the DataObject.SetData method.
<dxg:GridControl Name="gridControl1"> <!----> <dxg:GridControl.View> <dxg:TableView AllowDragDrop="True" StartRecordDrag="OnStartRecordDrag" /> </dxg:GridControl.View> </dxg:GridControl>
Retrieve serialized data that is transferred to the second application:
- Handle the DataViewBase.DropRecord event on the GridControl from the second application.
- Check whether the data is available using the DataObject.GetDataPresent method. The code sample below verifies that the DataFormats.Serializable data format is present in the transferred data object.
- Extract the data from the DropRecordEventArgs using the DataObject.GetData method.
- Modify dropped data and store them in the RecordDragDropData.
- Create the DataObject instance to initialize a new data object. Add the RecordDragDropData data to the created data object using the DataObject.SetData method.
- Assign the created data object to the DragEventArgsBase.Data property.
<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));
}