天天看点

c# web DataTable DataRow上下移动

1)前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvwTitles" runat="server">
        </asp:GridView>
        <asp:Button ID="btnUp" runat="server" Text="上移" οnclick="btnUp_Click" />
        <asp:Button ID="btnDown" runat="server" Text="下移" οnclick="btnDown_Click" />
    </div>
    </form>
</body>
</html>
           

2)后台

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace DataRowDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["dtTitles"] = null;
                GetData();
                BindData();
            }
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            if (ViewState["dtTitles"] != null)
            {
                gvwTitles.DataSource = (DataTable)ViewState["dtTitles"];
                gvwTitles.DataBind();
            }
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        private void GetData()
        {
            ViewState["dtTitles"] = SetData();
            //设置id为主键
            ((DataTable)ViewState["dtTitles"]).PrimaryKey = new DataColumn[] {((DataTable)ViewState["dtTitles"]).Columns["id"] };
        }
        protected void setRowToRow(DataRow dataRow, DataRow tempRow)
        {
            //tempRow["id"] = dataRow["id"];//主键唯一约束
            tempRow["type"] = dataRow["type"];
        }
        /// <summary>
        /// 上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUp_Click(object sender, EventArgs e)
        {
            DataRow currentRow = ((DataTable)ViewState["dtTitles"]).Rows.Find(102);//根据主键获取当前row
            DataRow[] rows = ((DataTable)ViewState["dtTitles"]).Select();//获取table中所有的row
            int currentIndex = Array.IndexOf(rows, currentRow); //获取当前row的index
            DataRow tempRow = ((DataTable)ViewState["dtTitles"]).NewRow();
            setRowToRow(((DataTable)ViewState["dtTitles"]).Rows[currentIndex - 1], tempRow);
            setRowToRow(((DataTable)ViewState["dtTitles"]).Rows[currentIndex], ((DataTable)ViewState["dtTitles"]).Rows[currentIndex - 1]);
            setRowToRow(tempRow, ((DataTable)ViewState["dtTitles"]).Rows[currentIndex]);
            BindData();
        }
        /// <summary>
        /// 下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDown_Click(object sender, EventArgs e)
        {
            DataRow currentRow = ((DataTable)ViewState["dtTitles"]).Rows.Find(102);
            DataRow[] rows = ((DataTable)ViewState["dtTitles"]).Select();
            int currentIndex = Array.IndexOf(rows, currentRow);
            DataRow tempRow = ((DataTable)ViewState["dtTitles"]).NewRow();
            setRowToRow(((DataTable)ViewState["dtTitles"]).Rows[currentIndex + 1], tempRow);
            setRowToRow(((DataTable)ViewState["dtTitles"]).Rows[currentIndex], ((DataTable)ViewState["dtTitles"]).Rows[currentIndex + 1]);
            setRowToRow(tempRow, ((DataTable)ViewState["dtTitles"]).Rows[currentIndex]);
            BindData();
        }
        /// <summary>
        /// 设置数据
        /// </summary>
        /// <returns></returns>
        private DataTable SetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("id"));
            dt.Columns.Add(new DataColumn("type"));
            DataRow dr1 = dt.NewRow();
            dr1["id"] = 101;
            dr1["type"] = "语文";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2["id"] = 102;
            dr2["type"] = "数学";
            dt.Rows.Add(dr2);
            DataRow dr3 = dt.NewRow();
            dr3["id"] = 103;
            dr3["type"] = "英语";
            dt.Rows.Add(dr3);
            return dt;
        }
    }
}