Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Change Row Position or Order in Asp.net Gridview using C#, VB.NET

Mar 23, 2015
Introduction:

Here I will explain how to change gridview row order or position on button click in asp.net using c#, vb.net or move asp.net gridview rows up and down with arrow button clicks in asp.net using c#vb.net.

Description
  
In Previous articles i explained many articles relating to gridview in asp.net. Now in this article I will explain how to change gridview row order or position on button click in in asp.net using c#vb.net on button click. 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
Now Design your aspx page like this

<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="&uArr;" ForeColor="White" Height="20px" Font-Bold="true" BackColor="#E07200" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
<asp:Button ID="btnDown" CommandName="Down" ToolTip="Down" Text="&dArr;" 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

Change Row Position or Order in Asp.net Gridview using C#, VB.NET
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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email
© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.