1.在Page_Load中调用BindGrid方法,实现在页面显示时数据按字段排序:
protected void Page_Load(object sender, EventArgs e)
{
//代码
Cache.Insert("RoomType",...);
BindGrid();
}
2. BindGrid方法实现排序
private void BindGrid()
{
DataSet roomtype = (DataSet)(Cache["RoomType"]);
DataView roomview = roomtype.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
{
roomview.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
GridView1.DataSource=roomview;
GridView1.DataBind();
}
3. 实现GridView1_Sorting方法
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
ViewState["sortdirection"] = "asc";
else
{
if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";
}
else
{
ViewState["sortdirection"] = "asc";
}
}
BindGrid();
GridView排序
最新推荐文章于 2013-05-28 17:00:59 发布