How to move datarow up and down in gridview in C# asp.net

Source code:-      

<asp:GridView ID="GridView1" Font-Names="Times New Roman" Font-Size="9pt" runat="server"
        AutoGenerateColumns="False" CellPadding="3" GridLines="None"
        BorderColor="White" BorderStyle="Ridge" BackColor="White"
        BorderWidth="2px" CellSpacing="1">
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:TextBox ID="txtname" runat="server" Text='<%# Eval("Name") %>'>  </asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Phone number">
                <ItemTemplate>
                    <asp:TextBox ID="txtphone" runat="server" Text='<%# Eval("Phone") %>'>   </asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnUp" Width="30px" 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="30px" 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="#C6C3C6" ForeColor="Black" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#594B9C" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#33276A" />
    </asp:GridView>

Design:-




C# code:-

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {          //Test Records       
            GridView1.DataSource = Enumerable.Range(1, 5).Select(a => new
            {
                Name = String.Format("Name {0}", a),
                Phone = String.Format("Phone {0}", a),
            });
            GridView1.DataBind();
        }
    }
    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.DataSource = rows.Select(a => new
   {
       Name = ((TextBox)a.FindControl("txtname")).Text,
       Phone = ((TextBox)a.FindControl("txtphone")).Text,
   }).ToList();
        GridView1.DataBind();
    }
}


How to move datarow up and down in gridview in C# asp.net How to move datarow up and down in gridview in C# asp.net Reviewed by NEERAJ SRIVASTAVA on 1:09:00 PM Rating: 5

No comments:

Powered by Blogger.