大家好,欢迎来到IT知识分享网。
Gridview 点击排序字段, 对分页后的数据根据排序字段进行升降排序
GridView 我用的是默认分页 , 排序的代码如下:
protected void gvResult_Sorting(object sender, GridViewSortEventArgs e)
{
GridView gv = (GridView)sender;
SortOrder = SortOrder == “asc” ? “desc” : “asc”;
SortColumn = e.SortExpression;
ViewState[“sortstr”] = SortColumn + ” ” + SortOrder;
DataTable dt = ViewState[“dt”] as DataTable;
DataCount = dt.Rows.Count;
DataView dv = new DataView(dt);
gv.DataSource = dv;
if (ViewState[“sortstr”] != null && ViewState[“sortstr”].ToString().Length > 0)
dv.Sort = ViewState[“sortstr”].ToString();
gv.DataBind();
}
protected void gvResult_Sorted(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
for (int i = 0; i < gv.Columns.Count; i++)
if (gv.Columns[i].SortExpression == SortColumn)
gv.HeaderRow.Cells[i].Controls.Add(new Literal() { Text = SortOrder == “asc” ? “↑” : “↓” });
}
int DataCount
{
get
{
var obj = ViewState[“DataCount”];
if (obj != null)
return (int)obj;
return 0;
}
set
{
ViewState[“DataCount”] = value;
}
}
string SortOrder
{
get
{
var obj = ViewState[“SortOrder”];
if (obj != null)
return (string)obj;
return “asc”;
}
set
{
ViewState[“SortOrder”] = value;
}
}
string SortColumn
{
get
{
var obj = ViewState[“SortColumn”];
if (obj != null)
return (string)obj;
return string.Empty;
}
set
{
ViewState[“SortColumn”] = value;
}
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/14196.html