Locate the record that is to be moved using the CommandArgument while processing the GridView.ItemCommand event. I make a copy of that DataTableRow, then delete the original one, accept changes on the DataTable, then re-insert it at the desired position (whether moving up or down). This approach can pose concurrency issues upon saving the table to the database because of the primary keys sequence.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reorder.aspx.cs" Inherits="Reorder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" DataKeyNames="ISBN" runat="Server" AutoGenerateColumns="false"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Move">
<ItemTemplate>
<asp:Button ID="btnUp" runat="Server" Text="Up" CommandName="MoveUp" CommandArgument='<%# Eval("ISBN") %>' />
<asp:Button ID="btnDown" runat="Server" Text="Down" CommandName="MoveDown" CommandArgument='<%# Eval("ISBN") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Publisher" HeaderText="Name" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Reorder : System.Web.UI.Page
{
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridViewView();
}
}
private void BindGridViewView()
{
if (Session["strTemp"] != null)
{
GridView1.DataSource = Session["strTemp"] as DataTable;
GridView1.DataBind();
}
else
{
GridView1.DataSource = GetCustomMadeDataTable();
GridView1.DataBind();
}
}
public DataTable GetCustomMadeDataTable()
{
//Create a new DataTable object
System.Data.DataTable objDataTable = new System.Data.DataTable();
//Create three columns with string as their type
objDataTable.Columns.Add("ISBN", typeof(Int32));
objDataTable.Columns.Add("Title", typeof(string));
objDataTable.Columns.Add("Publisher", typeof(string));
objDataTable.Columns.Add("Year", typeof(string));
DataColumn[] dcPk = new DataColumn[1];
dcPk[0] = objDataTable.Columns["ISBN"];
objDataTable.PrimaryKey = dcPk;
objDataTable.Columns["ISBN"].AutoIncrement = true;
objDataTable.Columns["ISBN"].AutoIncrementSeed = 1;
//Adding some data in the rows of this DataTable
DataRow dr;
for (int i = 1; i <= 5; i++)
{
dr = objDataTable.NewRow();
dr[1] = "Title" + i.ToString();
dr[2] = "Publisher" + i.ToString();
dr[3] = "200" + i.ToString();
objDataTable.Rows.Add(dr);
}
for (int i = 6; i <= 8; i++)
{
dr = objDataTable.NewRow();
dr[1] = "Computer" + i.ToString();
dr[2] = "TMH" + i.ToString();
dr[3] = "200" + i.ToString();
objDataTable.Rows.Add(dr);
}
Session["strTemp"] = objDataTable;
return objDataTable;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
dt = Session["strTemp"] as DataTable;
Int32 rowID = int.Parse(e.CommandArgument.ToString());
Int32 rowIndex = 0;
DataRow rowToBeMoved = dt.NewRow(); //create a copy of the row to be moved
for (Int16 i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["ISBN"].Equals(rowID))
{
rowIndex = i;
rowToBeMoved["ISBN"] = dt.Rows[i]["ISBN"];
rowToBeMoved["Publisher"] = dt.Rows[i]["Publisher"];
break;
}
}
switch (e.CommandName)
{
case "MoveUp":
if (rowIndex > 0)
{
//delete the selected row
dt.Rows[rowIndex].Delete();
dt.AcceptChanges();
//add the rowToBeMoved
dt.Rows.InsertAt(rowToBeMoved, rowIndex - 1);
dt.AcceptChanges();
GridView1.SelectedIndex = rowIndex - 1;
BindGridViewView();
}
break;
case "MoveDown":
if (rowIndex < dt.Rows.Count - 1)
{
dt.Rows[rowIndex].Delete();
dt.AcceptChanges();
dt.Rows.InsertAt(rowToBeMoved, rowIndex + 1);
dt.AcceptChanges();
GridView1.SelectedIndex = rowIndex + 1;
BindGridViewView();
}
break;
}
Session["strTemp"] = dt;
}
}