Move up and Down in GridView - Geeks to Go Forums

Jump to content

Log in Register Register Malware removal guide How it works

Move up and Down in GridView

#1 ali.B

  • Group: Malware Removal
  • Posts: 2,874
  • Joined: 04-January 09

Posted 01 April 2012 - 09:04 AM

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>
</head>
<body>
    <form id="form1" runat="server">
    <Menu:AdminMenu ID="AdminMenu1" runat="server" />
    <div>
        <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" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="Category_NAME" HeaderText="Category_NAME" 
                    SortExpression="Category_NAME" />
                <asp:HyperLinkField DataNavigateUrlFields="Category_ID" 
                    DataNavigateUrlFormatString="add_category_cover.aspx?Category_ID={0}" 
                    Text="Add/Change Cover" />
                    <asp:TemplateField>
                <ItemTemplate>
                    <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;" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <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;" />
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            <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:GridView>
        <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">
            <DeleteParameters>
                <asp:Parameter Name="Category_ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Category_NAME" Type="String" />
                <asp:Parameter Name="cover" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Category_NAME" Type="String" />
                <asp:Parameter Name="cover" Type="String" />
                <asp:Parameter Name="Category_ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
    </div>
    </form>
</body>
</html>


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.Add(row);
                else
                    rows.Insert(row.RowIndex - 1, row);
                break;
            case "Down":
                //If Last Item, insert at beginning (rotating positions)  
                if (row.RowIndex.Equals(GridView1.Rows.Count - 1))
                    rows.Insert(0, row);
                else
                    rows.Insert(row.RowIndex + 1, row);
                break;
        }
        GridView1.DataBind();
    }
}


Share this topic: