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;
}
}
有什么问题。
GridView有排序的功能噢,或者就是用SQL排序之后再去重新绑定。