实现Gridview行上下移动代码思路:

把要上移或下移的id 与它的临近行的id进行互换

效果:

前台页面:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView2.aspx.cs" Inherits="WebApplication3.GridView2" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
12 <div>
13 <asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
14 <ContentTemplate>
15 <table>
16 <tr>
17 <td>
18 <asp:Button Text="Up" runat="server" ID="btn_Up" OnClick="btn_Up_Click" />
19 <asp:Button Text="Down" runat="server" ID="btn_Down" OnClick="btn_Down_Click" />
20 </td>
21 </tr>
22 <tr>
23 <td>
24 <asp:GridView runat="server" ID="GridView1" OnRowDataBound="grid_RowBound">
25 <Columns>
26 <asp:TemplateField>
27 <ItemTemplate>
28 <input type="checkbox" runat="server" id="cb" value='<%# Eval("id") %>' />
29 </ItemTemplate>
30 </asp:TemplateField>
31 </Columns>
32 </asp:GridView>
33 </td>
34 </tr>
35 </table>
36 </ContentTemplate>
37 </asp:UpdatePanel>
38 </div>
39 </form>
40 </body>
41 </html>

后台代码:

  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Data;
8 namespace WebApplication3
9 {
10 public partial class GridView2 : System.Web.UI.Page
11 {
12 #region
13 public int? index = null;
14 int? tep = null;
15 #endregion
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 if (!IsPostBack)
19 {
20 DataTable dt = CreateDataTable();
21 GridView1.DataSource = dt; GridView1.DataBind();
22 }
23 }
24 public DataTable CreateDataTable()
25 {
26 DataTable dt = new DataTable();
27 dt.Columns.Add("id", typeof(Int32));
28 dt.Columns.Add("name", typeof(String));
29 dt.Columns.Add("age", typeof(Int32));
30 for (int i = 0; i < 6; i++)
31 {
32 DataRow dr = dt.NewRow();
33 dr["id"] = i;
34 dr["name"] = "name" + i.ToString(); ;
35 dr["age"] = 12 + i;
36 dt.Rows.Add(dr);
37 }
38 ViewState["dataTable"] = dt;
39 return dt;
40 }
41 #region UP
42 public void btn_Up_Click(object sender, EventArgs e)
43 {
44 try
45 {
46
47 DataTable da = (DataTable)ViewState["dataTable"];
48 for (int i = 0; i < GridView1.Rows.Count; i++)
49 {
50 System.Web.UI.HtmlControls.HtmlInputCheckBox cb = (System.Web.UI.HtmlControls.HtmlInputCheckBox)GridView1.Rows[i].FindControl("cb");
51 if (cb.Checked == true)
52 {
53 index = Convert.ToInt32(cb.Value);
54 break;
55 }
56 }
57 if (index - 1 < 0)
58 {
59 ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "", "alert('TOP!');", true);
60
61 }
62 else if (index == null)
63 {
64 ScriptManager.RegisterClientScriptBlock(this.updatePanel, this.updatePanel.GetType(), "", "alert('PLEASE CHECK ONE!');", true);
65 }
66 else
67 {
68 DataRow[] dCurrent = da.Select(string.Format("id={0}", index));
69 DataRow[] dUp = da.Select(string.Format("id={0}", index - 1));
70 tep = Convert.ToInt32(dUp[0]["id"]);
71 dUp[0]["id"] = dCurrent[0]["id"];
72 dCurrent[0]["id"] = tep;
73 DataView dv = ((DataTable)ViewState["dataTable"]).DefaultView;
74 dv.Sort = "id";
75 GridView1.DataSource = dv;
76 GridView1.DataBind();
77 updatePanel.Update();
78 index = null;
79 }
80 ViewState["dataTable"] = da;
81 }
82 catch (Exception ex)
83 {
84 throw ex;
85 }
86 }
87 #endregion
88 #region DOWN
89 public void btn_Down_Click(object sender, EventArgs e)
90 {
91 try
92 {
93 int? index = null;
94 DataTable da = (DataTable)ViewState["dataTable"];
95 for (int i = 0; i < GridView1.Rows.Count; i++)
96 {
97 System.Web.UI.HtmlControls.HtmlInputCheckBox cb = (System.Web.UI.HtmlControls.HtmlInputCheckBox)GridView1.Rows[i].FindControl("cb");
98 if (cb.Checked == true)
99 {
100 index = Convert.ToInt32(cb.Value);
101 break;
102 }
103 }
104 if (index + 1 > GridView1.Rows.Count - 1)
105 {
106 ScriptManager.RegisterClientScriptBlock(this.updatePanel, this.updatePanel.GetType(), "", "alert('END!');", true);
107 }
108 else if (index == null)
109 {
110 ScriptManager.RegisterClientScriptBlock(this.updatePanel, this.updatePanel.GetType(), "", "alert('PLEASE CHECK ONE!');", true);
111 }
112 else
113 {
114 DataRow[] dCurrent = da.Select(string.Format("id={0}", index));
115 DataRow[] dUp = da.Select(string.Format("id={0}", index + 1));
116 tep = Convert.ToInt32(dUp[0]["id"]);
117 dUp[0]["id"] = dCurrent[0]["id"];
118 dCurrent[0]["id"] = tep;
119 DataView dv = ((DataTable)ViewState["dataTable"]).DefaultView;
120 dv.Sort = "id";
121 GridView1.DataSource = dv;
122 GridView1.DataBind();
123 updatePanel.Update();
124 index = null;
125 tep = null;
126 }
127 ViewState["dataTable"] = da;
128 }
129 catch (Exception ex)
130 {
131 throw ex;
132 }
133 }
134 #endregion
135
136 #region GV RowBound
137 public void grid_RowBound(object sender, GridViewRowEventArgs e)
138 {
139 try
140 {
141 if (e.Row.RowType == DataControlRowType.DataRow)
142 {
143 if (tep != null)
144 {
145 System.Web.UI.HtmlControls.HtmlInputCheckBox cb = (System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Row.FindControl("cb");
146 if (tep == Convert.ToInt32(cb.Value))
147 {
148 cb.Checked = true;
149 }
150 }
151 }
152 }
153 catch (Exception ex)
154 {
155 throw ex;
156 }
157 }
158 #endregion
159 }
160 }

 

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"