本文共 7189 字,大约阅读时间需要 23 分钟。
今天没有什么事做,就写了一个GridView行上下移动的小例子。
方法有好多种,我先写第一种。
没有什么技术含量
思路:
把要上移或下移的id 与它的临近行的id进行互换
效果:
前台页面:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView2.aspx.cs" Inherits="WebApplication3.GridView2" %> 2 3 4 5 6 740 418 9 10
后台代码:
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 }
本文转自lpxxn博客园博客,原文链接:http://www.cnblogs.com/li-peng/archive/2011/11/14/2248683.html,如需转载请自行联系原作者