Click here to Skip to main content
Licence CPOL
First Posted 9 Dec 2007
Views 93,580
Downloads 3,043
Bookmarked 54 times

Drag and drop GridView items for ordering, in ASP.NET 2.0

By | 9 Dec 2007 | Article
Drag and drop GridView items for re-ordering of the items.

Screenshot -

Screenshot -

Introduction

In DragAndDropGridView, the user can rearrange a grid item via dragging and dropping. The user can drag a gridview item and then drop it on the item where he/she wishes to swap with. This control is very helpful for large grid items where we require to order items within the grid.

Using the code

Add the control to the toolbar or add a reference to Utility.dll in your project. Then, bind with the data source which you need to order. Now, add the DragAndDropEvent handler. DragAndDropEventArgs gives you the start index (source row index) and the EndIndex (destination row index). With these indices, update the data source and rebind the grid.

How this control works

This control is inherits from the GridView so that we can benefit from the GridView features.

HandleRow event

Handle the mouse down and mouse up events of each row in the gridview:

protected override void OnRowDataBound(GridViewRowEventArgs e)
{
    base.OnRowDataBound(e);
   //set the java script method for the dragging

    e.Row.Attributes.Add("onmousedown", "startDragging" + 
                         this.ClientID + "(" + e.Row.RowIndex + ",this); ");
    e.Row.Attributes.Add("onmouseup", "stopDragging" + 
                         this.ClientID + "(" + e.Row.RowIndex + "); ");
}

Whenever the user clicks on a grid item, a div is added in the body and the innerHTML of the div copies the selected item. Now, set the document mouse move event for the moving item.

document.onmousemove = function ()
{
    if(__item" + this.ClientID + @" != null){
        __item" + this.ClientID + @".style.left=  
          window.event.clientX + document.body.scrollLeft + 
          document.documentElement.scrollLeft ;
        __item" + this.ClientID + @".style.top =   
          window.event.clientY + document.body.scrollTop + 
          document.documentElement.scrollTop ;
      }
}

When the user ends up on another grid item, we call stopDragging(). This method calls the post back event.

Register the post back event script

The __doPostBack method is not added into the page until we register the post back event on the client-side by calling the Page.ClientScript.GetPostBackEventReference(this, "") method, which will create the __doPostback method on the client side.

Raise the post back event

Handle the RaisePostBackEvent of the control for raising the drag and drop events:

protected override void RaisePostBackEvent(string eventArgument)
{
    base.RaisePostBackEvent(eventArgument);
    //Handle the post back event

    //and set the values of the source and destination items

    if (Page.Request["__EVENTARGUMENT"] != null && 
        Page.Request["__EVENTARGUMENT"] != "" && 
        Page.Request["__EVENTARGUMENT"].StartsWith("GridDragging"))
    {
        char[] sep = { ',' };
        string[] col = eventArgument.Split(sep);
        DragAndDrop(this, new DragAndDropEventArgs(Convert.ToInt32(col[1]), 
                    Convert.ToInt32(col[2])));
    }
}

DragAndDropEventArgs

This EventArgs contains the start and the end index for the dragging event:

public class DragAndDropEventArgs : EventArgs
{
    private int _startIndex;
    /// <summary>
    /// Index of the source item
    /// </summary>

    public int StartIndex
    {
        get { return _startIndex; }
    }

    private int _endIndex;
    /// <summary>
    /// Index of the destination item
    /// </summary>

    public int EndIndex
    {
        get { return _endIndex; }

    }
    public DragAndDropEventArgs(int startIndex, int endindex)
    {
        _startIndex = startIndex;
        _endIndex = endindex;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rupesh Burad

Team Leader
MPS Technologies Limited
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionDrag And Drop GridView Jquery Pinmemberamit_jain_online22:56 22 Apr '12  
AnswerRe: Drag And Drop GridView Jquery PinmemberRupesh Burad18:31 23 Apr '12  
GeneralMy vote of 1 PinmemberDaniel Groh1:04 18 Jul '11  
QuestionProblem with MasterPages and Content Sections... :( PinmemberZubair5309:42 20 Jun '11  
Questiondraganddrop event doesn't fire in vb.net? Pinmemberkelmeister10:37 10 Feb '11  
GeneralCSS or SkinID for DragAndDropGridView PinmemberRodrigo Esparza5:58 3 Nov '10  
GeneralPostback do not raise with drag and drop, only raise event on simple click.... (I'm using the sample code) PinmemberRodrigo Esparza5:09 21 Oct '10  
GeneralRe: Postback do not raise with drag and drop, only raise event on simple click.... (I'm using the sample code) PinmemberRodrigo Esparza6:08 21 Oct '10  
GeneralRe: Postback do not raise with drag and drop, only raise event on simple click.... (I'm using the sample code) [modified] PinmemberRupesh Burad18:13 21 Oct '10  
GeneralRe: Postback do not raise with drag and drop, only raise event on simple click.... (I'm using the sample code) PinmemberRupesh Burad18:51 21 Oct '10  
GeneralRe: Postback do not raise with drag and drop, only raise event on simple click.... (I'm using the sample code) [modified] PinmemberRodrigo Esparza4:36 22 Oct '10  
GeneralRe: Postback do not raise with drag and drop, only raise event on simple click.... (I'm using the sample code) PinmemberRupesh Burad22:55 23 Oct '10  
GeneralMasterPage PinmemberMember 45068453:22 14 Aug '10  
GeneralRe: MasterPage PinmemberRupesh Burad18:50 15 Aug '10  
Generalin Paging its not working Pinmemberumesh095:54 30 Jun '10  
GeneralRe: in Paging its not working PinmemberRupesh Burad20:54 30 Jun '10  
Generalchanging checkbox triggers drag drop, Checkbox value not available in codebehind Pinmembermicheljansen3:10 1 Sep '09  
GeneralRe: changing checkbox triggers drag drop, Checkbox value not available in codebehind PinmemberRichard Johansson9:20 15 Sep '09  
GeneralRe: changing checkbox triggers drag drop, Checkbox value not available in codebehind Pinmembermicheljansen7:51 16 Sep '09  
GeneralRe: changing checkbox triggers drag drop, Checkbox value not available in codebehind PinmemberRupesh Burad19:39 19 Oct '09  
QuestionHow to make it work with a gridview and a sqldatasource PinmemberKaeghl11:48 27 Aug '09  
QuestionCant seem to get draganddrop event to fire [modified] PinmemberMatt Jorgenson12:44 28 Jun '09  
GeneralDrag and Drop drop in a paged view of grid control Pinmembergayatri.neelema1:10 13 Jan '09  
GeneralRe: Drag and Drop drop in a paged view of grid control PinmemberRupesh Burad23:58 31 May '09  
GeneralDragDropGridView MaterPage Pinmembergretafan0:38 17 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 10 Dec 2007
Article Copyright 2007 by Rupesh Burad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid