RadControls for ASP.NET AJAX
RadGrid exposes flexible event-driven mechanism to drag and drop grid records to reorder them within the same grid, move them to different grid instance or drop them over other html element on the page. In order to enable drag and drop of grid items, you need to set the two boolean grid properties to true, namely:
<Selecting AllowRowSelect="True" />
This will make the grid data rows draggable and the end user will be able to relocate them if needed. Additionally, you can define a GridDragDropColumn in your GridTableView's Columns collection. This will make your grid items draggable only when grabbed by the drag handle in the GridDragDropColumn. For a live demo, please refer to the RadGrid Items Drag-and-Drop QSF example.
Furthermore, depending on the position you drag an item (above or below other record) it
will be placed respectively above or below the corresponding grid item. This is meaningful only
when you reorder rows within the same RadGrid or from one RadGrid to another.
The event-driven model which allows you to process and complete the drag and drop operation can be
separated into two phases: client-side and server-side phase.
On mobile devices the row drag-drop and scrolling features in the grid are performed by the same touch gesture: dragging of the
content area of the grid. This imposes a limitation when both features are enabled on touch devices because it cannot be exclusively
determined which one of the two should be performed. One way to distinguish between scrolling and row drag-drop on mobile devices is to
use a GridDragDropColumn - this way the dragging of the rows will be performed only when you drag a row by the icon in the
GridDragDropColumn and on the rest of the content area scrolling will be performed.
There are three client grid events exposed to handle drag/drop action:
(cancelable), OnRowDropping (cancelable) and
The OnRowDragStarted event can be intercepted if you want to perform some conditional check and determine whether to cancel the drag operation or not. The syntax of the event handler follows the general client-side event signature of RadGrid for ASP.NET AJAX. The row which is about to be dragged can be accessed through the get_gridDataItem() property of the second argument passed in the OnRowDragStarted handler.
The OnRowDropping event should be attached to identify the target element on which the dragged grid record is dropped. If this element does not meet your criteria for acceptable target, cancel the operation by setting args.set_cancel(true) where args is the second argument passed to the OnRowDropping handler. Additionally, to determine the destination element or set it explicitly use the get_destinationHtmlElement() and set_destinationHtmlElement() properties that can be accessed through the args argument in the handler. Again, the syntax of the event handler follows the general client-side event signature of RadGrid for ASP.NET AJAX.
The OnRowDropped event can be handled if you would like to execute some extra code logic prior to the server-side OnRowDrop event rising. This event cannot be cancelled and have the same set of arguments as the OnRowDropping client event.
On the server there is a single event (named OnRowDrop). Subscribing to this event allows you to reorder the items in the source grid or remove them and append these rows to a destination grid instance. The sequence of actions you will have to undertake in order to change the source structure may vary because this depends strictly on the underlying data source and its data model. The common logic in all cases, however, is that you can use three arguments passed in the handler to accomplish the task:
e.HtmlElement - holds the html element (or grid item)
e.DestDataItem - the destination grid item object (either GridDataItem or GridNoRecordsItem)
e.DraggedItems - a collection of GridDataItems which holds the rows that are taking part in the current drop operation
e.DestinationGrid - a reference to the grid instance to which the row has been dragged to
e.DestinationTableView - a reference to the table to which the row has been draggged to, points to the MasterTableView or detail table in hierarchical grid
Combining the client and server part completes the circle and separates logically each part of the drag and drop process until it is finalized. For richer end user experience you can ajaxify the grid via RadAjaxManager and use RadAjaxLoadingPanel indicators. Otherwise the drag and drop operation will be performed with plain postback.
Note that with single row selection enabled (AllowMultiRowSelection = "false") the items will be automatically selected when a drag action is triggered. With multi-row selection enabled (AllowMultiRowSelection = "true") a prerequisite is first to select row(s) and then drag to reorder them/drop them over other grid/html element.
Below is a code extraction from the
relevant online demo in the RadGrid QSF: