随笔 - 25, 文章 - 2, 评论 - 2, 阅读 - 14046
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2024年12月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

GridView中的双向排序

Posted on   庞滨  阅读(175)  评论(0编辑  收藏  举报
1. 设置GridView 的属性 AllowSorting="True" 否则没有可排序的下横线.
2. 邦定要序排的字段
<asp:BoundField HeaderText="货号" DataField="ch_MaterialID" SortExpression="ch_MaterialID">
                                
<ItemStyle Height="10px" Width="100px" />
                            
</asp:BoundField>
3. 写排序的事件代码:
        //在排序发生前激发
        protected void GVStorageMaterial_Sorting(object sender, GridViewSortEventArgs e)
        
{
            
string strPage = e.SortExpression.ToString();
            
switch (strPage)
            
{
                
case "ch_ImageID":
                    ViewState[
"SortOrder"= "ch_ImageID";
                    
break;
                
case "ch_MaterialID":
                    ViewState[
"SortOrder"= "ch_MaterialID";
                    
break;
                
case "ch_MaterialName":
                    ViewState[
"SortOrder"= "ch_MaterialName";
                    
break;
            }

            
            
//string sPage = e.SortExpression;
            if (ViewState["OrderDire"] == null)
            {
                ViewState["OrderDire"] = "ASC";
            }
            if (ViewState["OrderDire"].ToString() == "ASC")
                ViewState["OrderDire"] = "DESC";
            else
                ViewState["OrderDire"] = "ASC";
            string strSQLSel = StrSqlSearch();
            this.InitializeGridview(strSQLSel);
        }

4. 在显示GridView的视图中设置排序的属性.
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

bool flagSel = objDBAccess.SearchSQL(strSQLSel, out objDS, out strMessage);
            
if (flagSel)
            
{
                DataView dv
=objDS.Tables[0].DefaultView;
                dv.Sort 
= strSort;
                GV.DataSource 
= dv;
                GV.DataBind();
            }
点击右上角即可分享
微信分享提示