Linq学习——gridview实现双向排序

前端代码:

添加一个gridview,将AllowSorting属性设置为true,在gridview每一列的SortExpression属性中添加相应的值,生成的代码如下:

            AutoGenerateColumns="False" onsorting="GridView1_Sorting">
       
                            SortExpression="EmployeeID" />
                            SortExpression="LastName" />
                            SortExpression="FirstName" />
           
                            SortExpression="TitleOfCourtesy" />
       

       

后台调用代码:

 

 private void BindEmp()
        {
            List localDataTable;
            localDataTable = objCategories.FindAllList(ViewState["SortOrder"].ToString(), ViewState["OrderDire"].ToString());
            GridView1.DataSource = localDataTable;
            GridView1.DataBind();            
        }

    gridview 的Sorting 事件代码:

 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string spage = e.SortExpression;
            if (ViewState["SortOrder"].ToString() == spage)
            {
                if (ViewState["OrderDire"].ToString() == "DESC")
                {
                    ViewState["OrderDire"] = "ASC";
                }
                else
                {
                    ViewState["OrderDire"] = "DESC";
                }
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
            }
            BindEmp();
        }

 protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "EmployeeID";
                ViewState["OrderDire"] = "ASC";
                BindEmp();
            }
        }

Bll层代码:

 public List FindAllList(string sordCol, string sortType)
        {
            return objDataAccess.FindAllList(sordCol, sortType); 
        }

DLL层代码:

public List FindAllList(string sordCol, string sortType)
        {
            List objList=new List();
            IEnumerable custQuery = null;
            switch (sordCol)
            {
                case "EmployeeID":
                    {
                        if (sortType == "ASC")
                        {
                            custQuery =
                            from em in nw.Employees
                            orderby em.EmployeeID
                            select em;
                        }
                        else if (sortType == "DESC")
                        {
                            custQuery =
                            from em in nw.Employees
                            orderby em.EmployeeID descending
                            select em;
                        }
                    }
                    break;
                case "LastName":
                    {
                        if (sortType == "ASC")
                        {
                            custQuery =
                            from em in nw.Employees
                            orderby em.LastName
                            select em;
                        }
                        else if (sortType == "DESC")
                        {
                            custQuery =
                            from em in nw.Employees
                            orderby em.LastName descending
                            select em;
                        }
                    }
                    break;
                case "FirstName":
                    {
                        if (sortType == "ASC")
                        {
                            custQuery =
                                from em in nw.Employees
                                orderby em.FirstName
                                select em;
                        }
                        else if (sortType == "DESC")
                        {
                            custQuery =
                                from em in nw.Employees
                                orderby em.FirstName descending
                                select em;
                        }
                    }
                    break;
                case "Title":
                    {
                        if (sortType == "ASC")
                        {
                            custQuery =
                                from em in nw.Employees
                                orderby em.Title
                                select em;
                        }
                        else if (sortType == "DESC")
                        {
                            custQuery =
                                from em in nw.Employees
                                orderby em.Title descending
                                select em;
                        }
                    }
                    break;
                default:
                    {
                        if (sortType == "ASC")
                        {
                            custQuery =
                                from em in nw.Employees
                                orderby em.TitleOfCourtesy
                                select em;
                        }
                        else if (sortType == "DESC")
                        {
                            custQuery =
                                from em in nw.Employees
                                orderby em.TitleOfCourtesy descending
                                select em;
                        }
                    }
                    break;
            }

            foreach (Employees cou in custQuery)
            {
                objList.Add(cou);
            }
            return objList.ToList();
        }

 

你可能感兴趣的:(Linq学习——gridview实现双向排序)