Click here to Skip to main content
15,886,080 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
4.07/5 (12 votes)
9 Dec 2007CPOL1 min read 203.1K   8.4K   59   47
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:

C#
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.

JavaScript
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:

C#
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:

C#
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)


Written By
Team Leader Nagarro Software Pvt Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to make it work with a gridview and a sqldatasource Pin
Kaeghl27-Aug-09 11:48
Kaeghl27-Aug-09 11:48 
QuestionCant seem to get draganddrop event to fire [modified] Pin
Matt Jorgenson28-Jun-09 12:44
Matt Jorgenson28-Jun-09 12:44 
GeneralDrag and Drop drop in a paged view of grid control Pin
gayatri.neelema13-Jan-09 1:10
gayatri.neelema13-Jan-09 1:10 
GeneralRe: Drag and Drop drop in a paged view of grid control Pin
Rupesh Burad31-May-09 23:58
Rupesh Burad31-May-09 23:58 
GeneralDragDropGridView MaterPage Pin
gretafan17-Dec-08 0:38
gretafan17-Dec-08 0:38 
AnswerRe: DragDropGridView MaterPage Pin
Zonkd81-Jun-12 10:26
Zonkd81-Jun-12 10:26 
Generalusing DragDropGridView in ShowModalDialog Pin
gate.6.6.625-Sep-08 11:40
gate.6.6.625-Sep-08 11:40 
GeneralRe: using DragDropGridView in ShowModalDialog Pin
Seth Smith21-May-09 11:31
Seth Smith21-May-09 11:31 
I am trying to use this code in a ModalPopupExtender. I have gotten it to function correctly no problem. The issue I have is that the "row" text that drags with the mouse on selecting an item is "behind" the modalpopup, so there is no visual indicator that I have an item in the mousedown state.

Any way to resolve this? It works fine, just need a visual indicator of some sort that the user can see when the mousedown state is active in a modalpopupextender....
Generaldrag and drop the rows in gridview Pin
chinnuk22-Sep-08 0:35
chinnuk22-Sep-08 0:35 
GeneralRe: drag and drop the rows in gridview Pin
gate.6.6.625-Sep-08 11:46
gate.6.6.625-Sep-08 11:46 
QuestionFirefox Compatable? Pin
razzzzer21-Aug-08 14:02
razzzzer21-Aug-08 14:02 
AnswerRe: Firefox Compatable? Pin
Rupesh Burad19-Oct-09 19:44
Rupesh Burad19-Oct-09 19:44 
GeneralUsing DragDropGridView along with AjaxControlToolkit Pin
DMCMA29-Jul-08 4:59
DMCMA29-Jul-08 4:59 
GeneralRe: Using DragDropGridView along with AjaxControlToolkit Pin
DMCMA29-Jul-08 5:00
DMCMA29-Jul-08 5:00 
GeneralUsing DragDropGridView along with AjaxControlToolkit Pin
DMCMA28-Jul-08 7:39
DMCMA28-Jul-08 7:39 
GeneralRe: Using DragDropGridView along with AjaxControlToolkit Pin
hnell23-Feb-11 0:48
hnell23-Feb-11 0:48 
Question"DragAndDrop" is null on drop action Pin
Erik Bertrand3-Jul-08 5:49
Erik Bertrand3-Jul-08 5:49 
AnswerRe: "DragAndDrop" is null on drop action Pin
Rupesh Burad3-Jul-08 21:08
Rupesh Burad3-Jul-08 21:08 
AnswerRe: "DragAndDrop" is null on drop action Pin
Erik Bertrand7-Jul-08 5:27
Erik Bertrand7-Jul-08 5:27 
GeneralRe: "DragAndDrop" is null on drop action Pin
izzyvef3-Feb-09 1:39
izzyvef3-Feb-09 1:39 
AnswerRe: "DragAndDrop" is null on drop action Pin
izzyvef3-Feb-09 1:37
izzyvef3-Feb-09 1:37 
Generaldrag row to another grid Pin
Member 5166589-May-08 9:20
Member 5166589-May-08 9:20 
GeneralRe: drag row to another grid Pin
Rupesh Burad2-Jun-08 0:16
Rupesh Burad2-Jun-08 0:16 
GeneralReOrder Grid [modified] Pin
Mark Henke18-Dec-07 6:58
Mark Henke18-Dec-07 6:58 
GeneralRe: ReOrder Grid Pin
Rupesh Burad15-Apr-08 23:37
Rupesh Burad15-Apr-08 23:37 

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

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