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 Binarytrue
.
Refer to the following Breaking Change for additional information: Component functionality changes due to Binary
#Implement Drag-and-Drop Between Applications
The following image demonstrates 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>
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:
- Handle the DataViewBase.DragRecordOver event of the GridControl from the second application.
- Call the DataObject.GetDataPresent method to check if dragged data is available. The example below allows you to drop only RecordDragDropData.
- Determine the operation type. 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>
#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.
Apply the SerializableAttribute to the data type involved in the drag-and-drop operation to indicate that instances of this type can be serialized:
Serialize data transferred from the first application:
- Handle the DataViewBase.StartRecordDrag event of 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 transferred to the second application:
- Handle the DataViewBase.DropRecord event of the GridControl from the second application.
- Call the DataObject.GetDataPresent method to check if data is available. The code sample below verifies that the transferred object stores data in the DataFormats.Serializable format.
- Call the DataObject.GetData method to extract data from DropRecordEventArgs.
- Modify dropped data and store it in RecordDragDropData.
- Create the DataObject instance to initialize a new data object. Add 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; } }
#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));
}