GridView排序以及对各行的控制

一、GridView排序

1.设置GridView允许排序AllowSorting="True"

2.设置排序字段SortExpression=排序字段
3.编写Sorting事件代码
//判断用户点击了哪个排序字段
          protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //判断用户点击了哪个排序字段
            //说明用户第台一次点击排序字段
            if (ViewState["sortexp"] == null)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add(e.SortExpression, "ASC");
                ViewState["sortexp"] = dic;
            }
            else//用户第台二次或者第台次点击排序字段
            {
                Dictionary<string, string> dic = ViewState["sortexp"] as Dictionary<string, string>;
                //判断用户本次点击的排序字段是否和上次点击的排序字段一致,如果一致的话,那么就更改此字段的排序规则,如果不是就清除上次的排序字段,添加新的排序字段和规则(这是根据一个字段排序的情况)
                if (dic.ContainsKey(e.SortExpression))
                {
                    //如果上次是升序,则改为降序,如果是降序则改为升序
                    if (dic[e.SortExpression] == "ASC")
                    {
                        dic[e.SortExpression] = "DESC";
                    }
                    else
                    {
                        dic[e.SortExpression] = "ASC";
                    }
                    //ViewState["sortexp"] = dic;
                }
                else//清除上次排序字段和规则,添加新的排序字段和规则
                {
                    dic.Clear();
                    dic.Add(e.SortExpression, "ASC");
                    //ViewState["sortexp"] = dic;
                }
            }
            //重新加载数据
            LoadData();




        }
4、更改LoadData()方法
string sqlstr = "SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY T1.Id DESC)AS rownumber, T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20)+'......' AS NewsContent,T1.CreateTime,T2.ClassName,T3.RealName  FROM T_News1 T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId)A WHERE A.rownumber>(@pageindex-1)*@pagesize AND A.rownumber<=@pageindex*@pagesize";
            if (ViewState["sortexp"]!=null)
            {
                Dictionary<string, string> dic = ViewState["sortexp"] as Dictionary<string, string>;
                string sqlorder = string.Empty;
                foreach (KeyValuePair<string,string> item in dic)
                {
                    sqlorder += item.Key + " " + item.Value+",";
                }
                sqlstr=sqlstr+" ORDER BY "+sqlorder.TrimEnd(',');
            }

            cmd.CommandText = sqlstr;


5.给GridView加上排序指示符,在RowDataBound事件中实现

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            #region 添加排序指示符
            if (e.Row.RowType == DataControlRowType.Header)//判断是否为标题行
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)//便利所有列
                {
                    if (e.Row.Cells[i].Controls.Count > 0)//控件的数量大于0
                    {
                        LinkButton link = e.Row.Cells[i].Controls[0] as LinkButton;
                        string sortexp = link.CommandArgument;
                        if (ViewState["sortexp"] != null)//只有当点击某个排序字段时,才会执行下面的语句
                        {
                            Dictionary<string, string> dic = ViewState["sortexp"] 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);
                            }
                        }
                    }
                }
            }
            #endregion




            #region 将刘晓飞创建的行的背景设置为突出色
            //设置背景色
            if (e.Row.RowType == DataControlRowType.DataRow)
            { 
                    if (e.Row.Cells[3].Text == "刘晓飞")
                    {
                        e.Row.BackColor = Color.DarkOrange;
                    }
            
            }
            #endregion


            #region 分别计算出每一页中创建者为刘晓飞和肖伟哲的新闻纪录的数量
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[3].Text == "刘晓飞")
                {
                    lcount++;
                }
                else if (e.Row.Cells[3].Text == "肖唯哲")
                {
                    xcount++;
                }


            }
            //将最后的结果输出到页脚
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = string.Format("刘晓飞{0},肖唯哲:{1}", lcount, xcount);
                //合并单元格,就是删除多余的单元格,然后让剩骸下的单元格占据所有单元格的宽度之和
                e.Row.Cells.RemoveAt(7);
                e.Row.Cells.RemoveAt(6);
                e.Row.Cells.RemoveAt(5);
                e.Row.Cells.RemoveAt(4);
                e.Row.Cells.RemoveAt(3);
                e.Row.Cells.RemoveAt(2);
                e.Row.Cells.RemoveAt(1);
                e.Row.Cells[0].ColumnSpan = 8;
                e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;


            }
            #endregion
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值