How to drag a file from the File Explorer and drop it to RadGridView
Environment
Product Version | Product | Author |
---|---|---|
2020.3.915 | RadGridView for WinForms | Nadya Karaivanova |
Description
A common requirement is to implement drag-drop behavior between controls. The first approach is to implement drag-drop operations between RadControls, e.g. between two RadGridViews or between RadGridView and RadTreeView, etc. In this case you can use the respective RadDragDropService. RadDragDropService represents a service that manages drag and drop actions in RadControls. According to the used control you can find the appropriate RadDragDropService, for example RadGridView handles the whole drag and drop operations by its RadGridViewDragDropService, RadTreeView offers TreeViewDragDropService, etc. A table with the respective RadControl and its RadDragDropService is available here
The second approach it to implement drag-drop operations between RadControls, e.g. RadGridView and a standart MS control, e.g. GridView, TreeView or File Explorer. In this case, it is suitable to use the Ole drag-and-drop functionality that Microsoft offers.
In this tutorial, I will demonstrate how you can achieve drag-and-drop functionality between File Explorer and RadGridView.
Solution
In order to achieve this, you should carefully follow the steps described here.
To perform a drop:
Set the AllowDrop property to true.
In the DragEnter event for the control where the drop will occur, ensure that the data being dragged is of an acceptable type by using the respective effect from DragDropEffects enumeration.
In the DragDrop event for the control is where the drop will occur. In order to detect which is the row the file is dropped to you can use the GetElementAtPoint method to check if the element is GridDataCellElement and get the row as well as the row index.
A full code snippet is demonstrated below