资源简介

GridView控件行数据上移、下移的实现,Visual Studio2010、Sqlserver2008。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender EventArgs e)
    {

    }
    protected void ArticleTypeGridView_RowCommand(object sender GridViewCommandEventArgs e)
    {
        GridViewRow row = (e.CommandSource as Control).Parent.Parent as GridViewRow;

        if (e.CommandName == “UP“)//单击了上移按钮 
        {
            if (row.RowIndex > 0)
            {
                //找到当前记录的TypeID和OrderNumber 
                int typeID = (int)ArticleTypeGridView.DataKeys[row.RowIndex].Value;
                int sequence = int.Parse((row.FindControl(“SequenceHiddenField“) as HiddenField).Value);

                GridViewRow prevRow = ArticleTypeGridView.Rows[row.RowIndex - 1];
                int prevTypeID = (int)ArticleTypeGridView.DataKeys[row.RowIndex - 1].Value;
                //上一条记录的TypeID和OrderNumber 
                int prevSequence = int.Parse((prevRow.FindControl(“SequenceHiddenField“) as HiddenField).Value);

                if (sequence == prevSequence)
                {
                    prevSequence += 1;
                }
                try
                {
                    string conStr = ConfigurationManager.ConnectionStrings[“GridviewUpDownDBConnectionString“].ToString();
                    SqlConnection con = new SqlConnection(conStr);
                    con.Open();
                    string sqlA = “ update ArticleType  set OrderNumber = “ + prevSequence + “ where TypeID =“ + typeID;
                    SqlCommand cmd = new SqlCommand(sqlA con);
                    cmd.ExecuteNonQuery();
                    string sqlB = “ update ArticleType  set OrderNumber = “ + sequence + “   where TypeID =“ + prevTypeID;
                    cmd.CommandText = sqlB;
                    cmd.ExecuteNonQuery();
                    con.Close();    
                    ArticleTypeGridView.DataBind();                 
                }
                catch (Exception ex)
                {
                                     
                }
            }
        }
         
        else if (e.CommandName == “Down“) //下移 
        {
            if (row.RowIndex < ArticleTypeGridView.Rows.Count - 1)
            {
                int typeID = (int)ArticleTypeGridView.DataKeys[row.RowIndex].Value;
                int sequence = int.Parse((row.FindControl(“SequenceHiddenField“) as HiddenField).Value);
                GridViewRow nextRow = ArticleTypeGridView.Rows[row.RowIndex + 1];
                int nextTypeID = (int)ArticleTypeGridView.DataKeys[row.RowIndex + 1].Value;
                int nextSequence = int.Parse((nextRow.FindControl(“SequenceHiddenField“) as HiddenField).Value);
                if (sequence == nextSequence)
           

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件          3  2012-03-06 10:41  GridView行数据上移下移\App_Code\Sqlconnection.cs

     文件    3145728  2012-03-06 11:10  GridView行数据上移下移\DB\GridviewUpDownDB.mdf

     文件    1048576  2012-03-06 11:10  GridView行数据上移下移\DB\GridviewUpDownDB_log.ldf

     文件       2039  2012-03-06 11:09  GridView行数据上移下移\Default.aspx

     文件       4097  2012-03-06 11:09  GridView行数据上移下移\Default.aspx.cs

     文件        511  2012-03-06 10:54  GridView行数据上移下移\web.config

     目录          0  2012-03-06 10:41  GridView行数据上移下移\App_Code

     目录          0  2012-03-06 10:37  GridView行数据上移下移\DB

     目录          0  2012-03-06 10:41  GridView行数据上移下移

----------- ---------  ---------- -----  ----

              4200954                    9


评论

共有 条评论