GridView如何进行排序

来源:互联网 发布:linux kill命令 编辑:程序博客网 时间:2024/05/18 18:00
 string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;        protected void Page_Load(object sender, EventArgs e)        {            fun();        }        public void fun()//给gridview绑定数据        {            using (SqlConnection con = new SqlConnection(strcon))            {                con.Open();                using (SqlCommand cmd = con.CreateCommand())                {                    string str = "select * from T_Food";                    #region 语句执行之前完善语句使语句可以实现排行                    if (ViewState["dic"]!=null)                    {                        Dictionary<string, string> dic = ViewState["dic"] as Dictionary<string, string>;                        string receive = string.Empty;                        foreach (KeyValuePair<string,string> item in dic)                        {                            receive += item.Key + " " + item.Value + ",";                        }                        str = str + " order by " + receive.TrimEnd(',');//完善语句                    }                    #endregion                    cmd.CommandText = str;                    SqlDataAdapter ad = new SqlDataAdapter(cmd);                    DataTable table = new DataTable();                    ad.Fill(table);                    GridView1.DataSource = table;                    GridView1.DataBind();                }            }        }        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)        {            //排序激发事件            if (ViewState["dic"] == null)//第一次激发排序            {                Dictionary<string, string> dic = new Dictionary<string, string>();                dic.Add(e.SortExpression, "asc");//设置排序方式                ViewState["dic"] = dic;//保存排序方式            }            else            {                Dictionary<string, string> dic = ViewState["dic"] as Dictionary<string, string>;                if (dic.ContainsKey(e.SortExpression))//判断是否是上次排序的字段                {                    if (dic[e.SortExpression] == "asc")                    {                        dic[e.SortExpression] = "desc";                    }                    else                    {                        dic[e.SortExpression] = "asc";                    }                }                else//清除上次的排序方法,从新排序                {                    dic.Clear();                    dic.Add(e.SortExpression, "asc");                }            }            fun();                    }        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)        {            //数据绑定后激发            //添加上升下降排序图标            if (e.Row.RowType==DataControlRowType.Header)            {                for (int i = 0; i < e.Row.Cells.Count; i++)                {                    if (e.Row.Cells[i].Controls.Count>0)                    {                        LinkButton link = e.Row.Cells[i].Controls[0] as LinkButton;                        string sortexp = link.CommandArgument;                        if (ViewState["dic"]!=null)//如果排序方法不为空执行添加                        {                            Dictionary<string, string> dic = ViewState["dic"] as Dictionary<string, string>;                            if (dic.ContainsKey(sortexp))                            {                                Literal li = new Literal();                                if (dic[sortexp] == "asc")                                {                                    li.Text = "↑";                                }                                else                                {                                    li.Text = "↓";                                }                                e.Row.Cells[i].Controls.Add(li);                            }                        }                    }                }            }        }