Introduction:
In this article I will explain how to move gridview rows up and down with arrow button clicks in asp.net.
Description:
Now
Design your aspx page like this
In this article I will explain how to move gridview rows up and down with arrow button clicks in asp.net.
Description:
Previous I explained many articles relating
to gridview in asp.net. Now in this article I
will explain how to move gridview rows up and down with button clicks in
asp.net. For that first design one table in database and give name as MobileDetails as shown
below
Column Name
|
Data Type
|
Allow Nulls
|
MobileId
|
int
|
No
|
MobileName
|
varchar(50)
|
Yes
|
Priority
|
int
|
Yes
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Move Gridview Rows up and down button clicks in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:GridView ID="gvUserInfo"
runat="server"
AutoGenerateColumns="false"
DataKeyNames="Priority"
OnRowCommand="gvUserInfo_RowDataCommand"
>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="MobileId" HeaderText="MobileId" />
<asp:BoundField DataField="MobileName" HeaderText="MobileName" />
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Button ID="btnUp"
CommandName="Up"
ToolTip="UP"
Text="⇑"
ForeColor="White"
Height="20px"
Font-Bold="true"
BackColor="#E07200"
runat="server"
CommandArgument="<%# ((GridViewRow)
Container).RowIndex %>" />
<asp:Button ID="btnDown"
CommandName="Down"
ToolTip="Down"
Text="⇓"
ForeColor="White"
Height="20px"
Font-Bold="true"
BackColor="#E07200"
runat="server"
CommandArgument="<%# ((GridViewRow)
Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
After completion of aspx page add following namespaces in
codebehind
C#
Code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
|
After
that add following code in code behind
SqlConnection con = new SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
SqlCommand cmd;
SqlDataAdapter da;
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind
gridview from database
protected void
BindGridview()
{
con.Open();
cmd = new SqlCommand("select
* from MobileDetails order by Priority asc", con);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
GridViewRow FirstRow = gvUserInfo.Rows[0];
Button btnUp = (Button)FirstRow.FindControl("btnUp");
btnUp.Enabled = false;
GridViewRow LastRow =
gvUserInfo.Rows[gvUserInfo.Rows.Count-1];
Button btnDown = (Button)LastRow.FindControl("btnDown");
btnDown.Enabled = false;
}
protected void
gvUserInfo_RowDataCommand(object sender, GridViewCommandEventArgs e)
{
int index=0;
GridViewRow gvrow;
GridViewRow previousRow;
if (e.CommandName == "Up")
{
index = Convert.ToInt32(e.CommandArgument);
gvrow = gvUserInfo.Rows[index];
previousRow = gvUserInfo.Rows[index - 1];
int mobilePriority = Convert.ToInt32(gvUserInfo.DataKeys[gvrow.RowIndex].Value.ToString());
int mobileId = Convert.ToInt32(gvrow.Cells[0].Text);
int previousId = Convert.ToInt32(previousRow.Cells[0].Text);
con.Open();
cmd = new SqlCommand("update
MobileDetails set Priority='" + (mobilePriority - 1) + "' where MobileId='" + mobileId + "'; update MobileDetails set Priority='"
+ (mobilePriority) + "' where
MobileId='" + previousId + "'",
con);
cmd.ExecuteNonQuery();
con.Close();
}
if (e.CommandName == "Down")
{
index = Convert.ToInt32(e.CommandArgument);
gvrow = gvUserInfo.Rows[index];
previousRow = gvUserInfo.Rows[index + 1];
int mobilePriority = Convert.ToInt32(gvUserInfo.DataKeys[gvrow.RowIndex].Value.ToString());
int mobileId = Convert.ToInt32(gvrow.Cells[0].Text);
int previousId = Convert.ToInt32(previousRow.Cells[0].Text);
con.Open();
cmd = new SqlCommand("update
MobileDetails set Priority='" + (mobilePriority + 1) + "' where MobileId='" + mobileId + "'; update MobileDetails set Priority='"
+ (mobilePriority) + "' where
MobileId='" + previousId + "'",
con);
cmd.ExecuteNonQuery();
con.Close();
}
BindGridview();
}
|
VB.NET Code
In
VB.NET CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" not supportable. Please change this one
to CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" in your aspx page. After that write
the following code in code behin
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Class _Default
Inherits System.Web.UI.Page
Private con As New SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Private cmd As
SqlCommand
Private da As
SqlDataAdapter
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
' This method is used to bind
gridview from database
Protected Sub
BindGridview()
con.Open()
cmd = New
SqlCommand("select * from MobileDetails
order by Priority asc", con)
da = New
SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
con.Close()
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
Dim FirstRow As GridViewRow =
gvUserInfo.Rows(0)
Dim btnUp As Button = DirectCast(FirstRow.FindControl("btnUp"), Button)
btnUp.Enabled = False
Dim LastRow As GridViewRow =
gvUserInfo.Rows(gvUserInfo.Rows.Count - 1)
Dim btnDown As Button = DirectCast(LastRow.FindControl("btnDown"), Button)
btnDown.Enabled = False
End Sub
Protected Sub
gvUserInfo_RowDataCommand(ByVal sender As Object, ByVal e As
GridViewCommandEventArgs)
Dim index As Integer = 0
Dim gvrow As GridViewRow
Dim previousRow As GridViewRow
If e.CommandName = "Up"
Then
index = Convert.ToInt32(e.CommandArgument)
gvrow = gvUserInfo.Rows(index)
previousRow = gvUserInfo.Rows(index - 1)
Dim mobilePriority As Integer =
Convert.ToInt32(gvUserInfo.DataKeys(gvrow.RowIndex).Value.ToString())
Dim mobileId As Integer = Convert.ToInt32(gvrow.Cells(0).Text)
Dim previousId As Integer =
Convert.ToInt32(previousRow.Cells(0).Text)
con.Open()
cmd = New
SqlCommand("update MobileDetails set
Priority='" & (mobilePriority - 1) & "' where MobileId='" & mobileId
& "'; update MobileDetails set
Priority='" & (mobilePriority) & "'
where MobileId='" & previousId & "'",
con)
cmd.ExecuteNonQuery()
con.Close()
End If
If e.CommandName = "Down"
Then
index = Convert.ToInt32(e.CommandArgument)
gvrow = gvUserInfo.Rows(index)
previousRow = gvUserInfo.Rows(index + 1)
Dim mobilePriority As Integer =
Convert.ToInt32(gvUserInfo.DataKeys(gvrow.RowIndex).Value.ToString())
Dim mobileId As Integer = Convert.ToInt32(gvrow.Cells(0).Text)
Dim previousId As Integer =
Convert.ToInt32(previousRow.Cells(0).Text)
con.Open()
cmd = New
SqlCommand("update MobileDetails set
Priority='" & (mobilePriority + 1) & "' where MobileId='" & mobileId
& "'; update MobileDetails set
Priority='" & (mobilePriority) & "'
where MobileId='" & previousId & "'",
con)
cmd.ExecuteNonQuery()
con.Close()
End If
BindGridview()
End Sub
End Class
|
|
Demo
Download
Sample Code Attached
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
14 comments :
good and very nice site in all site.good matter with example.so nice and really true examplefor asp.net.
thanks for suresh......
Shiv Singh krishnawat
asp.net Programmerhead
HI,
Your example is working fine. But what if I have sorting enabled with pageddatasource. Then it is not working. It would be kind of you to show how to move the row up and down with sorting enabled.
Thanks,
please provide any suggestion to write stored procedures for the exact same example. I really like this email.
Thanks,
K
int mobilePriority = Convert.ToInt32(gvUserInfo.DataKeys[gvrow.RowIndex].Value.ToString());
Input string was not in a correct format.
giving this error. please let me know
hello sir,
Everything is working except one.when I am doing up and down then in database only Priority is changing
rest all detail are not.I want Id also up and down according to Priority then i can show it changed data on another page.
Please try to help me....pls sir..
thank you sir......
very nice article....
best site thanks suresh sir
Thanks suresh
thanks its very useful
nice article
fudu kam
'GridViewRow' is a type and cannot be used as an expression.
Done with ur code ! :) Thnx :)
Note: Only a member of this blog may post a comment.