Click here to Skip to main content
12,894,897 members (59,083 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 24 May 2009

Demonstration of Row Swapping in a Grid View

, 24 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
An article to demonstrate row swapping in a grid view


Once I came across a situation where the client wanted to swap the rows in a GridView. I will show a sample example of doing the same.

Straight to the Program

The datasource is basically a datatable with two columns viz. Name, Address:

DataTable dtsource = new DataTable();

//Adding columns to datatable

dtsource.Rows.Add("Name1", "Address1");
dtsource.Rows.Add("Name2", "Address2");
dtsource.Rows.Add("Name3", "Address3");
dtsource.Rows.Add("Name4", "Address4");
dtsource.Rows.Add("Name5", "Address5");
dtsource.Rows.Add("Name6", "Address6");
dtsource.Rows.Add("Name7", "Address7");
dtsource.Rows.Add("Name8", "Address8");
dtsource.Rows.Add("Name9", "Address9");
dtsource.Rows.Add("Name10", "Address10");

I consume the same for the GridView at the page load event.

I wrote the swapping logic in the Row Command event since it is the event that gets fired when a control (e.g. Button) is clicked that resides inside the GridView

Obviously, the Button’s CommandName as well as the Command Argument need to be set

In this case, the CommandName is set to Up & Down respectively while I pass the grid’s row id in the Command Argument.

CommandName="Up" CommandArgument=<%# Container.DataItemIndex %>

The Swap method whose signature is Swap(int rowid, string option) does the row swapping:

/// <summary>
/// Function name: Swap
/// Purpose: Swaps the grid's row
/// </summary>
/// current row id of the grid
/// Up or Down
private void Swap(int rowid, string option)
    // temp  = val1
    // val1 = val2
    //val2 = temp

    if (option == "UP")
        for (int i = 0; i <= 1; i++)
            string temp = grdRowMovementDemo.Rows[rowid - 1].Cells[i].Text;
            grdRowMovementDemo.Rows[rowid - 1].Cells[i].Text = 
            grdRowMovementDemo.Rows[rowid].Cells[i].Text = temp;
        for (int i = 0; i <= 1; i++)
            string temp = grdRowMovementDemo.Rows[rowid].Cells[i].Text;
            grdRowMovementDemo.Rows[rowid].Cells[i].Text = 
			grdRowMovementDemo.Rows[rowid + 1].Cells[i].Text;
            grdRowMovementDemo.Rows[rowid + 1].Cells[i].Text = temp;

I wrote the calling function in the RowCommand event of the GridView. It is as under:

int rowid = Convert.ToInt32(e.CommandArgument);

if (e.CommandName == "Up")
    if (rowid > 0) // Rowid == 0 indicates that the top most row is reached
    Swap(rowid, "UP");

if (e.CommandName == "Down")
    if (rowid != grdRowMovementDemo.Rows.Count - 1)	// the bottom most row
						// is reached
    Swap(rowid, "DOWN");


I have shown one of the ways by which we can achieve the row movement in the GridView. Any better suggestion/solution is always welcome.


  • 24th May, 2009: Initial post


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


About the Author

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
P.Salini27-Apr-12 20:36
memberP.Salini27-Apr-12 20:36 
Questionthe data switch doesn't appear Pin
schaerdz29-Jan-12 21:37
memberschaerdz29-Jan-12 21:37 
Questiontest new message Pin
waziristan22-Jan-12 20:25
memberwaziristan22-Jan-12 20:25 
GeneralThanks for the really useful article. I have modified the Swap method to actually modify the underlying data (if using a DataSet) Pin
steveprowland28-Nov-09 6:02
membersteveprowland28-Nov-09 6:02 
GeneralMy vote of 1 Pin
michael.d10-Jul-09 6:49
membermichael.d10-Jul-09 6:49 
GeneralList of Entities Pin
alhambra-eidos26-May-09 20:25
memberalhambra-eidos26-May-09 20:25 
GeneralRe: List of Entities Pin
Niladri_Biswas27-May-09 0:36
memberNiladri_Biswas27-May-09 0:36 
GeneralObservation and suggestion Pin
JeffCirceo26-May-09 8:12
memberJeffCirceo26-May-09 8:12 
GeneralThis does not change the underlying datasource Pin
zlezj24-May-09 21:16
memberzlezj24-May-09 21:16 
GeneralRe: This does not change the underlying datasource Pin
Niladri_Biswas27-May-09 0:39
memberNiladri_Biswas27-May-09 0:39 
GeneralRe: This does not change the underlying datasource Pin
zlezj27-May-09 22:40
memberzlezj27-May-09 22:40 
GeneralRe: This does not change the underlying datasource Pin
Irwan Hassan18-Jun-09 4:16
memberIrwan Hassan18-Jun-09 4:16 
GeneralRe: This does not change the underlying datasource Pin
zlezj18-Jun-09 4:35
memberzlezj18-Jun-09 4:35 
GeneralRe: This does not change the underlying datasource Pin
waziristan22-Jan-12 20:23
memberwaziristan22-Jan-12 20:23 
GeneralRe: This does not change the underlying datasource Pin
waziristan22-Jan-12 20:24
memberwaziristan22-Jan-12 20:24 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 24 May 2009
Article Copyright 2009 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid