GridView da Satırların Yerini Butona Tıklayarak Aşağı Yukarı Kaydırmak

 

Merhaba arkadaşlar bu makalemizde GridView daki satırların bulunduğu sıradaki pozisyonlarını yine GridView a ekleyeceğimiz Yukarı, Aşağı butonlarıyla istenilen pozisyona hareket ettireceğiz. 


İlk önce GridView ın AutoGenerateColumns="False" yapın. Sonrasında columns kısmına sütunlarımızı ve itemtemplate kısmınada Aşağı yukarı hareketi sağlayacak imagebuttonlarımızı ekleyelim. 

 

Screenshot















Şekil 1


 

 














Şekil 2

 

WebForm1.aspx.cs

using System;

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace gridview_change_row_position

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                bindList();

            }

          

        }

 

        protected void bindList()

        {

 

            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\person.mdf;Integrated Security=True");

 

            {

                con.Open();

                SqlCommand cmd = new SqlCommand("Select * From [dbo].[person] Order by Priority Asc", con);

                SqlDataAdapter da = new SqlDataAdapter(cmd);

                DataSet ds = new DataSet();

                da.Fill(ds);

                con.Close();

                GridView1.DataSource = ds;

                GridView1.DataBind();

                GridViewRow firstRow = GridView1.Rows[0];

                ImageButton btnUp = (ImageButton)firstRow.FindControl("btnUp");

                btnUp.Enabled = false;

                GridViewRow lastRow = GridView1.Rows[GridView1.Rows.Count - 1];

                ImageButton btnDown = (ImageButton)lastRow.FindControl("btnDown");

                btnDown.Enabled = false;

 

            }

        }

 

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

        {

            int index = 0;

            GridViewRow gvrow;

            GridViewRow previousRow;

            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\person.mdf;Integrated Security=True");

            if (e.CommandName == "Up")

            {

                index = Convert.ToInt32(e.CommandArgument);

                gvrow = GridView1.Rows[index];

                previousRow = GridView1.Rows[index - 1];

                int iPriority = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());

                int sId = Convert.ToInt32(gvrow.Cells[0].Text);

                int previousId = Convert.ToInt32(previousRow.Cells[0].Text);

                

                con.Open();

                SqlCommand cmd = new SqlCommand("Update [dbo].[person] set Priority='" + (iPriority - 1) + "' Where Id='" + sId + "'; Update [dbo].[person] set Priority='" + (iPriority) + "' Where Id='" + previousId + "'", con);

                cmd.ExecuteNonQuery();

                con.Close();

            }

            if (e.CommandName == "Down")

            {

                index = Convert.ToInt32(e.CommandArgument);

                gvrow = GridView1.Rows[index];

                previousRow = GridView1.Rows[index + 1];

                int iPriority = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());

                int sId = Convert.ToInt32(gvrow.Cells[0].Text);

                int previousId = Convert.ToInt32(previousRow.Cells[0].Text);

                con.Open();

                SqlCommand cmd = new SqlCommand("Update [dbo].[person] set Priority='" + (iPriority + 1) + "' Where Id='" + sId + "'; Update [dbo].[person] set Priority='" + (iPriority) + "' Where Id='" + previousId + "'", con);

                cmd.ExecuteNonQuery();

                con.Close();

            }

            bindList(); 

 

        }

    }

}

    

 

WebForm.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridview_change_row_position.WebForm1" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

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

        <div>

           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Priority" 

                     OnRowCommand="GridView1_RowCommand" CellPadding="4" GridLines="Horizontal" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" Width="362px">

             <Columns>

             

                  <asp:BoundField DataField="Id" HeaderText="Id" />

                  <asp:BoundField DataField="Name" HeaderText="Name" />

                  <asp:BoundField DataField="Surname" HeaderText="Surname" />

 

                  <asp:TemplateField HeaderText="Location">

                  <ItemTemplate>

                  

                       <asp:ImageButton ID="btnUp" CommandName="Up" ToolTip="Up" Text="&uArr;" ForeColor="White" Height="35px" ImageUrl="~/img/sort-up.png" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"  />

                    <asp:ImageButton ID="btnDown" CommandName="Down" ToolTip="Down" Text="&dArr;" ForeColor="White" Height="35px" ImageUrl="~/img/sort-down.png" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"  />

                   

                  </ItemTemplate>

                  </asp:TemplateField>

                 

               

          </Columns>

                        <FooterStyle BackColor="White" ForeColor="#333333" />

                        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

                        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />

                        <RowStyle BackColor="White" ForeColor="#333333" />

                        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />

                        <SortedAscendingCellStyle BackColor="#F7F7F7" />

                        <SortedAscendingHeaderStyle BackColor="#487575" />

                        <SortedDescendingCellStyle BackColor="#E5E5E5" />

                        <SortedDescendingHeaderStyle BackColor="#275353" />

      </asp:GridView>

        </div>

       

    </form>

</body>

</html>

      

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN

 

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...