gridview如何对内容排序
如图吧,数据库里有数据顺序的字段,怎么能点击那个向上或者向下的箭头,就把2条数据的顺序对调?以前没怎么接触过这些东西,麻烦大家了~ --------------------编程问答-------------------- GV有点击标题排序的属性的 --------------------编程问答-------------------- 不是点击标题排序,是点那个向上或者向下的箭头,然后让这2个条目顺序对换~ --------------------编程问答-------------------- 我以前是用javacript来动态排序
你可以参考下这篇文章
http://www.cnblogs.com/ocean2000/archive/2007/07/09/810799.html --------------------编程问答-------------------- 以前做过这个,把点击的东西update就行了,应该注意的是如果是第一个获最后一个要不能update --------------------编程问答-------------------- Insus.NET分析与写的和你的点不一样
参考:
--------------------编程问答-------------------- 内啥,我这有数据结构和一半的程序,谁有功夫帮我改改啊?QQ:756231808,愁啊…… --------------------编程问答-------------------- 设置一个排序列,更改相应的位置就可以了! --------------------编程问答-------------------- from:http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
void UpdateProcessTaxis(int iOriginalTaxisNumber, int iNewTaxisNumber, int iMaxTaxisNumber, string strId)
{
if (iOriginalTaxisNumber != iNewTaxisNumber)
{
if (iNewTaxisNumber == 1 & iOriginalTaxisNumber == iMaxTaxisNumber)
{
objList.UpdateTaxis("+", 1, iMaxTaxisNumber - 1, InsusBase.CurrentMemberId); //从最大值排至第一
}
else if (iNewTaxisNumber == 1 & iOriginalTaxisNumber != iMaxTaxisNumber)
{
objList.UpdateTaxis("+", 1, iOriginalTaxisNumber - 1, InsusBase.CurrentMemberId); //从中间值排至第一
}
else if (1 < iNewTaxisNumber & iNewTaxisNumber < iOriginalTaxisNumber)
{
objList.UpdateTaxis("+", iNewTaxisNumber, iOriginalTaxisNumber - 1, InsusBase.CurrentMemberId); //中间值由大至小排序
}
else if (iNewTaxisNumber == iMaxTaxisNumber & iOriginalTaxisNumber == 1)
{
objList.UpdateTaxis("-", 2, iMaxTaxisNumber, InsusBase.CurrentMemberId); //从第一排至最大值
}
else if (iNewTaxisNumber == iMaxTaxisNumber & iOriginalTaxisNumber != 1)
{
objList.UpdateTaxis("-", iOriginalTaxisNumber + 1, iMaxTaxisNumber, InsusBase.CurrentMemberId); //从中间值排至最大
}
else if (iOriginalTaxisNumber < iNewTaxisNumber & iNewTaxisNumber < iMaxTaxisNumber)
{
objList.UpdateTaxis("-", iOriginalTaxisNumber + 1, iNewTaxisNumber, InsusBase.CurrentMemberId); //中间值由小至大排序
}
objList.UpdateTaxis(strId, iNewTaxisNumber);
}
}
3.GridView正反双向排序:
效果图:点姓名各2次的排序,点其他也一样可以。
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
//清清月儿的博客http://blog.csdn.net/21aspnet
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortOrder"] = "身份证号码";
ViewState["OrderDire"] = "ASC";
bind();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
bind();
}
public void bind()
{
string sqlstr = "select top 5 * from 飞狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "飞狐工作室");
DataView view = myds.Tables["飞狐工作室"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
GridView1.DataSource = view;
GridView1.DataBind();
sqlcon.Close();
}
}
前台主要代码:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
CellPadding="3" Font-Size="9pt" OnSorting="GridView1_Sorting" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
<asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
<asp:BoundField DataField="员工性别" HeaderText="性别" SortExpression="员工性别"/>
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
--------------------编程问答-------------------- 利用每行的ID
点向上的箭头ID就加一,点向下的箭头ID就减一,而表格始终按ID大小来排列就是了 --------------------编程问答-------------------- 楼上的动作太快了。。。 --------------------编程问答--------------------
DataSet ds=new DataSet ();--------------------编程问答-------------------- 我现在是这样做的
ds=……
DataView dv = ds.Tables[0].DefaultView;
dv.Sort = "pID desc"; //这个地方可以写 t-sql 的排序语句如: order by pID desc
GridView1.DataSource = dv;
GridView1.DataBind();
以下是负责调整顺序的:
<asp:ButtonField ButtonType="Image" CommandName="order_up" HeaderText="升" ImageUrl="~/1.jpg" />
<asp:ButtonField ButtonType="Image" CommandName="order_down" HeaderText="降" ImageUrl="~/1.jpg" />
后台里,
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "order_up")
Index_Up();
if (e.CommandName == "order_down")
Index_Down();
}
现在就是这2个方法不会写了,Index_Up()和Index_Down()这2个方法需要什么参数才能获取到是哪一行被修改啊?
补充:.NET技术 , ASP.NET