Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

Move up and Down in GridView

  • Please log in to reply



    Trusted Helper

  • Malware Removal
  • 3,086 posts
I have implemented a Move up and Down in my gridview table, its a slightly modified version from a code i found on the net, its not functioning i can't figure out why

.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/admin/Category/Category_View.aspx.cs" Inherits="Category_View" %>

<!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>My Control Panel</title>
    <form id="form1" runat="server">
    <Menu:AdminMenu ID="AdminMenu1" runat="server" />
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/admin/Category/Category_insert.aspx">Add new category</asp:HyperLink>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" DataKeyNames="Category_ID" DataSourceID="SqlDataSource1" 
            EmptyDataText="There are no data records to display." ForeColor="#333333" 
            <AlternatingRowStyle BackColor="White" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="Category_NAME" HeaderText="Category_NAME" 
                    SortExpression="Category_NAME" />
                <asp:HyperLinkField DataNavigateUrlFields="Category_ID" 
                    Text="Add/Change Cover" />
                    <asp:Button ID="btnUp" Width="24px" ForeColor="White" Height="20px" Font-Bold="true"
                        OnClick="MoveGridViewRows" ToolTip="Move Up" Font-Size="Medium" BorderStyle="None"
                        BackColor="#507CD1" CommandName="Up" runat="server" Text="&uArr;" />
                    <asp:Button ID="btnDown" Width="24px" ForeColor="White" Height="20px" Font-Bold="true"
                        OnClick="MoveGridViewRows" ToolTip="Move Down" Font-Size="Medium" BorderStyle="None"
                        BackColor="#507CD1" CommandName="Down" runat="server" Text="&dArr;" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AliConnectionString1 %>" 
            DeleteCommand="DELETE FROM [Category] WHERE [Category_ID] = @Category_ID" 
            InsertCommand="INSERT INTO [Category] ([Category_NAME], [cover]) VALUES (@Category_NAME, @cover)" 
            ProviderName="<%$ ConnectionStrings:AliConnectionString1.ProviderName %>" 
            SelectCommand="SELECT [Category_ID], [Category_NAME], [cover] FROM [Category]" 
            UpdateCommand="UPDATE [Category] SET [Category_NAME] = @Category_NAME, [cover] = @cover WHERE [Category_ID] = @Category_ID">
                <asp:Parameter Name="Category_ID" Type="Int32" />
                <asp:Parameter Name="Category_NAME" Type="String" />
                <asp:Parameter Name="cover" Type="String" />
                <asp:Parameter Name="Category_NAME" Type="String" />
                <asp:Parameter Name="cover" Type="String" />
                <asp:Parameter Name="Category_ID" Type="Int32" />
        <br />

Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Category_View : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    protected void MoveGridViewRows(object sender, EventArgs e)
        Button btnUp = (Button)sender;
        GridViewRow row = (GridViewRow)btnUp.NamingContainer;
        // Get all items except the one selected  
        var rows = GridView1.Rows.Cast<GridViewRow>().Where(a => a != row).ToList();
        switch (btnUp.CommandName)
            case "Up":
                //If First Item, insert at end (rotating positions)  
                if (row.RowIndex.Equals(0))
                    rows.Insert(row.RowIndex - 1, row);
            case "Down":
                //If Last Item, insert at beginning (rotating positions)  
                if (row.RowIndex.Equals(GridView1.Rows.Count - 1))
                    rows.Insert(0, row);
                    rows.Insert(row.RowIndex + 1, row);

  • 0


Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP