New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Drag and Drop of Grid Items

This article explains the drad-and-drop of grid items. It consists of the following sections.

Overview

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:

ASP.NET
<telerik:RadGrid runat="server" RenderMode="Lightweight">
    <ClientSettings AllowRowsDragDrop="true">
        <Selecting AllowRowSelect="True" />
    </ClientSettings>
</telerik:RadGrid>

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

Handling Drag-and-drop

There are two phases when the user drags and drops rows. The first is the client-side action in the browser, and if that is not cancelled by the developer, a postback is made so the server can be notified of the data change (e.g., so you can update your data sources). The information below explains what you can do in each phase.

Client-side phase

There are three client grid events exposed to handle drag/drop action: OnRowDragStarted (cancelable), OnRowDropping (cancelable) and OnRowDropped.

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

Server-side phase

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.

Scrolling

When dragging an item within the grid to reposition it, the grid can scroll automatically with the drag operation. The only requirement is that you enable scrolling. When configuring scrolling, you may also find useful the Height vs. ScrollHeight article. You can stop the automatic scrolling by setting ClientSettings.AllowAutoScrollOnDragDrop="false".

The user can drag an item to an arbitrary element on the page or another grid. The originatig grid cannot know about that other HTML structure or its scrolling setup, so scrolling for the drag-and-drop operation is available only within the source grid.

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 touse a GridDragDropColumn - this way the dragging of the rows will be performed only when you drag a row by the icon in theGridDragDropColumn and on the rest of the content area scrolling will be performed.

Row Selection

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.

grid itemsdragdrop itemselecting

Examples

Below is a code extraction from the item drag-and-drop online demo:

ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel" runat="server" Transparency="30" CssClass="LoadingPanel_Hay"></telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxManager runat="server" ID="radAjax" DefaultLoadingPanelID="RadAjaxLoadingPanel">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="grdPendingOrders">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="grdPendingOrders" />
                <telerik:AjaxUpdatedControl ControlID="grdShippedOrders" />
                <telerik:AjaxUpdatedControl ControlID="msg" />
            </UpdatedControls>
        </telerik:AjaxSetting>
        <telerik:AjaxSetting AjaxControlID="grdShippedOrders">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="grdPendingOrders" />
                <telerik:AjaxUpdatedControl ControlID="grdShippedOrders" />
                <telerik:AjaxUpdatedControl ControlID="msg" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadScriptBlock runat="server" ID="scriptBlock">
    <script type="text/javascript">
        function onRowDropping(sender, args) {
            if (sender.get_id() == "grdPendingOrders") {
                var node = args.get_destinationHtmlElement();
                if (!isChildOf('<%=grdShippedOrders.ClientID %>', node) && !isChildOf('<%=grdPendingOrders.ClientID %>', node)) {
                    args.set_cancel(true);
                }
            }
            else {
                var node = args.get_destinationHtmlElement();
                if (!isChildOf('trashCan', node)) {
                    args.set_cancel(true);
                }
                else {
                    if (confirm("Are you sure you want to delete this order?"))
                        args.set_destinationHtmlElement('trashCan');
                    else
                        args.set_cancel(true);
                }
            }
        }
         function isChildOf(parentId, element) {
            while (element) {
                if (element.id && element.id.indexOf(parentId) > -1) {
                    return true;
                }
                element = element.parentNode;
            }
            return false;
        }
    </script>
</telerik:RadScriptBlock>
  
<div class="exWrap">
     <p class="howto">Drag orders from pending to shipped when dispatched<br />
     Reorder pending orders on priority<br />
     Drop a shipped order over the recycle bin to delete it</p>
     <div style="float:left; padding:0 6px 0 10px">
         <h2 style="color:#9c3608">Pending Orders</h2>
         <telerik:RadGrid RenderMode="Lightweight" runat="server" ID="grdPendingOrders" Skin="Silk" OnNeedDataSource="grdPendingOrders_NeedDataSource"
             AllowPaging="True" Width="350px" OnRowDrop="grdPendingOrders_RowDrop"
             AllowMultiRowSelection="true">
             <MasterTableView DataKeyNames="OrderId">
             </MasterTableView>
             <ClientSettings AllowRowsDragDrop="True">
                   <Selecting AllowRowSelect="True" EnableDragToSelectRows="true"/>
                   <ClientEvents OnRowDropping="onRowDropping" />
             </ClientSettings>
         </telerik:RadGrid>
     </div>
     <div style="float:right; padding:0 10px 0 6px">
         <h2 style="color:#3c8b04">Shipped Orders</h2>
         <telerik:RadGrid RenderMode="Lightweight" runat="server" AllowPaging="True" ID="grdShippedOrders" Skin="Silk"
              OnNeedDataSource="grdShippedOrders_NeedDataSource" Width="350px"
              OnRowDrop="grdShippedOrders_RowDrop" AllowMultiRowSelection="false">
             <MasterTableView DataKeyNames="OrderId">
             </MasterTableView>
             <ClientSettings AllowRowsDragDrop="True">
                 <Selecting AllowRowSelect="True" EnableDragToSelectRows="false"/>
                 <ClientEvents OnRowDropping="onRowDropping" />
             </ClientSettings>
         </telerik:RadGrid>
     </div>
     <div style="clear:both;"><!-- --></div>
     <div class="exFooter">
         <div id="trashCan">Recycle Bin</div>
         <div class="exMessage" runat="server" id="msg" visible="false" enableviewstate="false">
              Order(s) succsesfully deleted!
         </div>
 </div>
</div>
C#
protected IList<Order> PendingOrders
{
    get
    {
        try
        {
            object obj = Session["PendingOrders"];
            if (obj == null)
            {
                obj = GetOrders();
                if (obj != null)
                {
                    Session["PendingOrders"] = obj;
                }
                else
                {
                    obj = new List<Order>();
                }
            }
            return (IList<Order>)obj;
        }
        catch
        {
            Session["PendingOrders"] = null;
        }
        return new List<Order>();
    }
    set
    {
        Session["PendingOrders"] = value;
    }
}
protected IList<Order> ShippedOrders
{
    get
    {
        try
        {
            object obj = Session["ShippedOrders"];
            if (obj == null)
            {
                Session["ShippedOrders"] = obj = new List<Order>();
            }
            return (IList<Order>)obj;
        }
        catch
        {
            Session["ShippedOrders"] = null;
        }
        return new List<Order>();
    }
    set
    {
        Session["ShippedOrders"] = value;
    }
}
protected void grdPendingOrders_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    grdPendingOrders.DataSource = PendingOrders;
}
protected IList<Order> GetOrders()
{
    IList<Order> results = new List<Order>();
    using (IDbConnection connection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection())
    {
        connection.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        using (IDbCommand command = connection.CreateCommand())
        {
            command.CommandText = "SELECT o.OrderID, o.CustomerID, o.RequiredDate, c.CompanyName FROM orders o INNER JOIN customers c on o.customerID = c.customerID";
            connection.Open();
            try
            {
                IDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    int id = (int)reader.GetValue(reader.GetOrdinal("OrderID"));
                    string customerID = (!reader.IsDBNull(reader.GetOrdinal("CustomerID"))) ? (string)reader.GetValue(reader.GetOrdinal("CustomerID")) : string.Empty;
                    DateTime requiredDate = (!reader.IsDBNull(reader.GetOrdinal("RequiredDate"))) ? (DateTime)reader.GetValue(reader.GetOrdinal("RequiredDate")) : DateTime.MinValue;
                    string companyName = (!reader.IsDBNull(reader.GetOrdinal("CompanyName"))) ? (string)reader.GetValue(reader.GetOrdinal("CompanyName")) : string.Empty;
                    results.Add(new Order(id, customerID, companyName, requiredDate.ToShortDateString()));
                }
            }
            catch
            {
                results.Clear();
            }
        }
    }
    return results;
}
protected void grdShippedOrders_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    grdShippedOrders.DataSource = ShippedOrders;
}
protected void grdPendingOrders_RowDrop(object sender, GridDragDropEventArgs e)
{
    if (string.IsNullOrEmpty(e.HtmlElement))
    {
        if (e.DraggedItems[0].OwnerGridID == grdPendingOrders.ClientID)
        {                    // items are drag from pending to shipped grid     
            if ((e.DestDataItem == null && ShippedOrders.Count == 0) || e.DestDataItem != null && e.DestDataItem.OwnerGridID == grdShippedOrders.ClientID)
            {
                IList<Order> shippedOrders = ShippedOrders;
                IList<Order> pendingOrders = PendingOrders;
                foreach (GridDataItem draggedItem in e.DraggedItems)
                {
                    Order tmpOrder = GetOrder(pendingOrders, (int)draggedItem.GetDataKeyValue("OrderId"));
                    if (tmpOrder != null)
                    {
                        shippedOrders.Add(tmpOrder);
                        pendingOrders.Remove(tmpOrder);
                    }
                }
                ShippedOrders = shippedOrders;
                PendingOrders = pendingOrders;
                grdPendingOrders.Rebind();
                grdShippedOrders.Rebind();
            }
            else if (e.DestDataItem != null && e.DestDataItem.OwnerGridID == grdPendingOrders.ClientID)
            {                        //reorder items in pending  grid   
                IList<Order> pendingOrders = PendingOrders;
                Order order = GetOrder(pendingOrders, (int)e.DestDataItem.GetDataKeyValue("OrderId"));
                int destinationIndex = pendingOrders.IndexOf(order);
                List<Order> ordersToMove = new List<Order>();
                foreach (GridDataItem draggedItem in e.DraggedItems)
                {
                    Order tmpOrder = GetOrder(pendingOrders, (int)draggedItem.GetDataKeyValue("OrderId"));
                    if (tmpOrder != null)
                        ordersToMove.Add(tmpOrder);
                }
                foreach (Order orderToMove in ordersToMove)
                {
                    pendingOrders.Remove(orderToMove);
                    pendingOrders.Insert(destinationIndex, orderToMove);
                }
                PendingOrders = pendingOrders;
                grdPendingOrders.Rebind();
                e.DestDataItem.Selected = true;
            }
        }
    }
}
private static Order GetOrder(IEnumerable<Order> ordersToSearchIn, int orderId)
{
    foreach (Order order in ordersToSearchIn)
    {
        if (order.OrderID == orderId)
        {
            return order;
        }
    }
    return null;
}
protected void grdShippedOrders_RowDrop(object sender, GridDragDropEventArgs e)
{
    if (!string.IsNullOrEmpty(e.HtmlElement) && e.HtmlElement == "trashCan")
    {
        IList<Order> shippedOrders = ShippedOrders;
        bool deleted = false;
        foreach (GridDataItem draggedItem in e.DraggedItems)
        {
            Order tmpOrder = GetOrder(shippedOrders, (int)draggedItem.GetDataKeyValue("OrderId"));
            if (tmpOrder != null)
            {
                shippedOrders.Remove(tmpOrder);
                deleted = true;
            }
        }
        if (deleted)
        {
            msg.Visible = true;
        }
        ShippedOrders = shippedOrders;
        grdShippedOrders.Rebind();
    }
}

protected class Order
{
    private string _companyName;
    private string _customerId;
    private int _orderId;
    private string _date;
    public Order(int orderId, string customerId, string companyName, string requiredDate)
    {
        _orderId = orderId;
        _customerId = customerId;
        _companyName = companyName;
        _date = requiredDate;
    }
    public int OrderID
    {
        get
        {
            return _orderId;
        }
    }
    public string CustomerID
    {
        get
        {
            return _customerId;
        }
    }
    public string Company
    {
        get
        {
            return _companyName;
        }
    }
    public string Date
    {
        get
        {
            return _date;
        }
    }
}