GridView如何进行排序

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值