Free Jobs
Reference




Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: How to move GridView Row Up and Down with Custom Button using RowCommand event

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share





User: dhiraj5079 Post Date: 29 Mar 2011 09:45

Introduction:
In this post I will be describing How to move GridView Row Up and Down with Custom
Up and Down Button.



Main Content:

The output image I am putting at start so that at one glance you will

understand what I am going to explain.See the out put Image below.




See the data table Image below that is used to bind the grid view.



We will be using the event GridView1_RowCommand to accomplish this task.

I have not given the datbase Update code from DAL layer because that is not Important here.


Write the below code in .aspx file


 <form id="form1" runat="server">

    <div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  OnRowCommand="GridView1_RowCommand" 

CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%">

        <Columns>


<asp:TemplateField HeaderText="Category">

  <EditItemTemplate>

 <asp:TextBox ID="TxtName" runat="server" Text=''<%# Bind("City") %>''></asp:TextBox>

 <asp:HiddenField ID="HdnName" runat="server" Value=''<%# Bind("City") %>''></asp:HiddenField>

 </EditItemTemplate>

<ItemTemplate>

  <asp:Label ID="LblName" runat="server" Text=''<%# Bind("City") %>''></asp:Label>                                   

</ItemTemplate>                                        

 <ItemStyle HorizontalAlign ="Left" />  <HeaderStyle HorizontalAlign="Left" />

</asp:TemplateField>




<asp:TemplateField HeaderText="Up Down">

  <ItemTemplate>

 <asp:LinkButton ID="BtnUp" runat="server" CommandName="Up" Text=''Up''

CommandArgument="<%# ((GridViewRow) Container).RowIndex %>">

</asp:LinkButton>&nbsp;&nbsp;&nbsp;

 <asp:LinkButton ID="BtnDown" runat="server" CommandName="Down" Text=''Down''

CommandArgument="<%# ((GridViewRow) Container).RowIndex %>">

</asp:LinkButton>

</ItemTemplate>

 <ItemStyle HorizontalAlign ="Left" />  <HeaderStyle HorizontalAlign="Left" />

</asp:TemplateField>


<asp:TemplateField HeaderText="DisplayID" Visible="false" >

 <ItemTemplate>

 <asp:Label ID="LblDisplayID" runat="server" Text=''<%# Bind("DisplayId") %>''></asp:Label>

 </ItemTemplate>

  <ItemStyle HorizontalAlign ="Left" />  <HeaderStyle HorizontalAlign="Left" />

  </asp:TemplateField>




  </Columns>

<RowStyle BackColor="white" Font-Names="Verdana" Font-Size="9px" ForeColor="black" />

 <FooterStyle BackColor="#e7e7e9" Font-Names="Verdana" Font-Size="11px" Font-Bold="True" ForeColor="White" HorizontalAlign="left" />

 <PagerStyle BackColor="#e7e7e9" Font-Names="Verdana" Font-Size="11px" ForeColor="black" HorizontalAlign="Center" VerticalAlign="top" />                           

 <HeaderStyle  BackColor="lightblue"  Font-Names="Verdana" Font-Size="11px" Font-Bold="true" ForeColor="White" />                           

 <AlternatingRowStyle BackColor="White" Font-Names="Verdana" Font-Size="9px" ForeColor="black" />

       

 </asp:GridView>

 </div>

 </form>


 public partial class GridView : System.Web.UI.Page

{

    DataSet dsUser = new DataSet();

    BAL balObject = new BAL();

    DAL Dal = new DAL();

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            string Qry = "Select * From TestUpDown Order BY SeqId";


            dsUser = Dal.ExecuteQueryDataSet(Qry);


            if (dsUser != null)

            {

                GridView1.DataSource = dsUser;

                GridView1.DataBind();

            }

        }

       


    }


   Write the below code at the aspx.cs file

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

    {

        string Qry = string.Empty;

        if (e.CommandName == "Up")

        {

            //ErrorMsg.Text = "";

            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

            Label DisplayID = (Label)row.FindControl("LblDisplayID");


            Qry = "Select SeqID from TestUpDown where DisplayID = ''" + DisplayID.Text + "''";

            string SeqNo = Dal.ExecuteQueryScaler(Qry);


            if (Convert.ToInt16(SeqNo) > 1)

            {

                Qry = "Select DisplayID from TestUpDown where SeqID = ''" + (Convert.ToInt16(SeqNo) - 1) + "''";

                string PreDisplayID = Dal.ExecuteQueryScaler(Qry);


                Qry = "Update TestUpDown Set SeqID = ''" + (Convert.ToInt16(SeqNo) - 1) + "'' Where DisplayID = ''" + DisplayID.Text + "'';";

                Qry += "Update TestUpDown Set SeqID = ''" + Convert.ToInt16(SeqNo) + "''

                Where DisplayID = ''" + Convert.ToInt16(PreDisplayID) + "'';";

                Dal.ExecuteQuery(Qry);

                FillGridView1();

              

            }

        }

        if (e.CommandName == "Down")

        {

            //ErrorMsg.Text = "";

            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

            Label DisplayID = (Label)row.FindControl("LblDisplayID");


            Qry = "Select Max(SeqID) from TestUpDown";

            string MaxSeqNo = Dal.ExecuteQueryScaler(Qry);


            Qry = "Select SeqID from TestUpDown where DisplayID = ''" + DisplayID.Text + "''";

            string SeqNo = Dal.ExecuteQueryScaler(Qry);


            if (Convert.ToInt16(MaxSeqNo) > Convert.ToInt16(SeqNo))

            {

                Qry = "Select DisplayID from TestUpDown where SeqID = ''" + (Convert.ToInt16(SeqNo) + 1) + "''";

                string PostDisplayID = Dal.ExecuteQueryScaler(Qry);


                Qry = "Update TestUpDown Set SeqID = ''" + (Convert.ToInt16(SeqNo) + 1) + "'' Where DisplayID = ''" + DisplayID.Text + "'';";

                Qry += "Update TestUpDown Set SeqID = ''" + Convert.ToInt16(SeqNo) + "'' Where DisplayID = ''" + Convert.ToInt16(PostDisplayID) + "'';";

                Dal.ExecuteQuery(Qry);

                FillGridView1();


              

            }

        }


    }


    private void FillGridView1()

    {

        string Qry = "Select * From TestUpDown Order BY SeqId";


        dsUser = Dal.ExecuteQueryDataSet(Qry);


        if (dsUser != null)

        {

            GridView1.DataSource = dsUser;

            GridView1.DataBind();

        }

    }

}




Conclusion:
Thus Up down functionality can be implemented using RowCommand event



About Writer:
Hi,I am IT Professional From Mumbai


Writer's Link: http://

Writer's Email:







Comments:
Post Comment





Similar Articles Top ArticlesLatest Articles Other Articles by this Author







www.freejobsreference.com Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)