首页 新闻 会员 周边 捐助

Gridview分页后排序

0
悬赏园豆:20 [已关闭问题]

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;  
            }  
        }  

happygirl_yj的主页 happygirl_yj | 初学一级 | 园豆:180
提问于:2010-07-20 18:02
< >
分享
其他回答(1)
0

有什么问题。

Astar | 园豆:40805 (高人七级) | 2010-07-21 09:24
0

GridView有排序的功能噢,或者就是用SQL排序之后再去重新绑定。

changbluesky | 园豆:854 (小虾三级) | 2010-07-21 10:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册