本文转自:http://blog.csdn.net/gmjinrong/article/details/4516301

GridView实现支持多列排序,并显示升、降序图标上网找了很多资料参考才解决了问题

现分享如下:

1.新建StyleSheet.css,图片自己准备及路径需自行更改.

.alternatingrowstyle{
   /* background-color:#f9f9f9;*/
   background-color:White;
}

.sortascheaderstyle{
    background:#F2F2F2 url(../Img/sort_asc.gif) no-repeat scroll right center;
    padding-left:5px;
}
.sortdescheaderstyle{
    background:#F2F2F2 url(../Img/sort_desc.gif) no-repeat scroll right center;
    padding-left:5px;
}

.ItemStyle
{
 
 
}

.gridview
 {
  word-break:break-all;
  word-wrap:break-word 
 }

headerstyle{
   /* background-color:#e5e5e5;*/
   background-color:#F2F2F2;
    height:23px;
}

.headerstyle th {
    border:1px solid #bbbbbb;
    padding-left:5px;
    text-align:left;

.headerstyle a {
    color:black;
}

.pagerstyle{
    color:#444444;
    background-color:#e5e5e5;
    font-size:x-small;
    text-align:right;
}

2.前台

引用:

<head runat="server">

<link href="css/StyleSheet.css" rel="Stylesheet" type="text/css" />

</head>

Gridview部分:

<asp:GridView ID="givOrder" runat="server" CssClass="gridview"
             AutoGenerateColumns="False" OnRowDataBound="givOrder_RowDataBound"
             DataKeyNames="Rowid" AllowSorting="True" 
             onsorting="givOrder_Sorting" >
           <AlternatingRowStyle CssClass="alternatingrowstyle" />
          <HeaderStyle CssClass="headerstyle" />
          <PagerStyle CssClass="pagerstyle" />
  
           <Columns>
                <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30px">
                    <ItemTemplate>
                        <%# Convert.ToInt32(DataBinder.Eval(Container, "DataItemIndex")) + 1 %>
                    </ItemTemplate>
                    <ItemStyle CssClass="ItemStyle"/>
                </asp:TemplateField>
               
                 <asp:TemplateField ItemStyle-HorizontalAlign="Center"
                    HeaderText="订单号" SortExpression="OrderNo">
                    <ItemTemplate>
                       <asp:TextBox ID="txtOrderNo" runat="server" TextMode="MultiLine"   CssClass="textBox" Rows="1"  Width="60px" Text='<%# Bind("orderNo") %>' ></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField ItemStyle-HorizontalAlign="Center"
                    HeaderText="客户" SortExpression="CustomerName" >
                    <ItemTemplate>
                        <asp:TextBox ID="txtCustomerName" runat="server"  TextMode="MultiLine"  CssClass="textBox" Rows="1"  Width="100px"  Text='<%# Bind("CustomerName") %>' ></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>

</Columns>
        </asp:GridView>

后台实现:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridViewDataBind();  //自己实现的数据绑定DataSource
                IDictionary<string, string> idic = new Dictionary<string, string>();
                ViewState["sortIDic"] = idic;

}
                     
       
        }

protected void givOrder_Sorting(object sender, GridViewSortEventArgs e)
    {

int cellIndex = -1;

foreach (DataControlField field in givOrder.Columns)
            {
                if (field.SortExpression == e.SortExpression)
                {
                    cellIndex =givOrder.Columns.IndexOf(field);
                    if (ViewState["cellindex"] != null)
                    {
                        int oldCellindex=int.Parse(ViewState["cellindex"].ToString());
                        if (oldCellindex != cellIndex)
                            givOrder.Columns[oldCellindex].HeaderStyle.CssClass = "";
                    }
                   
                    break;
                }
            }

string sortExpression = e.SortExpression;

IDictionary<string, string> idic = ViewState["sortIDic"] as IDictionary<string, string>;

if (!idic.ContainsKey(sortExpression))
            {
                idic.Add(e.SortExpression, e.SortDirection.ToString().Replace("Ascending", "ASC").Replace("Descending", "DESC"));
                givOrder.Columns[cellIndex].HeaderStyle.CssClass = "sortascheaderstyle";
          
            }
            else
            {
                string strSortDirection = idic[e.SortExpression];

if (strSortDirection == "ASC")
                {
                    idic[e.SortExpression] = "DESC";
                    givOrder.Columns[cellIndex].HeaderStyle.CssClass = "sortdescheaderstyle";

}
                else if (strSortDirection == "DESC")
                {
                    idic.Remove(e.SortExpression);
                    givOrder.Columns[cellIndex].HeaderStyle.CssClass = "";
                 
                }
            }

ViewState["sortIDic"] = idic;
            ViewState["cellindex"] = cellIndex;

SortGridView();

}

/// <summary>
        /// 实现多行排序
        /// </summary>
          private void SortGridView()
        {
            StringBuilder sbSortExpression = new StringBuilder();
         
             IDictionary<string, string> idic = ViewState["sortIDic"] as IDictionary<string, string>;
             string[] strkeys = new string[idic.Count];

if (idic.Count > 0)
             {
                 idic.Keys.CopyTo(strkeys, 0);
                 for (int i = 0; i <idic.Count; i++)
                 {
                     sbSortExpression.Append(strkeys[i]);
                     sbSortExpression.Append(" ");
                     sbSortExpression.Append(idic[strkeys[i]]);

if (i != idic.Count-1)
                         sbSortExpression.Append(", ");

}
            
             }

clsborder l_order = new clsborder();
             DataTable dt = l_order.SelectOrder();
          
             DataView dv = dt.DefaultView;

dv.Sort = sbSortExpression.ToString();
            
             givOrder.DataSource = dv;
             givOrder.DataBind();
       
        }

[转]Gridview实现多列排序,并显示图标的更多相关文章

  1. GridView多列排序

    public class WebGridView:GridView { 属性#region 属性 /**//// <summary> /// 是否启用或者禁止多列排序 /// </s ...

  2. ASP.NET 为GridView添加序号列,且支持分页连续累计显示

    为GridView添加序号列,且支持分页连续累计显示,废话不多说,直接上代码: <%@ Page Language="C#" AutoEventWireup="tr ...

  3. python 全栈开发,Day114(装饰器,排序规则,显示列,添加按钮,定制ModelForm,自定义列表页面,自定制URL)

    一.装饰器 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事务处理, ...

  4. 详解ASP.NET4 GridView的四种排序样式

    与ASP.NET 的其他Web控件一能够,Gridview控件拥有很多不同的CSS样式属性设置,包括象CssClass,Font字体,ForeColor,BackColor,BackColor, Wi ...

  5. Jtable 表格按多列排序(支持中文汉字排序)

    这两天公司让做一个Jtable表格的排序,首先按A列排序,在A列相等时按B列排序,B列相等时按C列排序,ABC三列可以任意指定,最多分三列,这样的一个需求.由于我是大神,所以必须做了出来.ok,不自恋 ...

  6. 自定义多列排序:C++/Java实现

    前言: 有些时候,我们在编程中会遇到多列排序的需求.假如在execle,这事儿就太easy了.不过没办法,现在就需要你用Java或者C++实现这样一个功能! 比如将下表无序的数据通过重排之后按照以下规 ...

  7. 向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题

    原文:向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题 GridView的一个模板列中的内容是按钮,需要实现以下的效果: GridView分页显示数据,点击编辑按钮(模 ...

  8. 如何让Gridview在没有数据的时候显示表头(asp.net)

    原文:如何让Gridview在没有数据的时候显示表头(asp.net) 1.前言 当对GridView控件进行数据绑定时,如果绑定的记录为空,网页上就不显示GridView,造成页面部分空白,页面布局 ...

  9. BootstrapTable的列排序怎么搞

    1.BootstrapTable的列排序怎么搞. 先搞一个table,使用ajax将数据查询出来,然后可以在所有列都加上排序.满足自己的需求. data-sortable="true&quo ...

随机推荐

  1. Dalsa线扫相机SDK开发-小试牛刀(1)

    拿到了dalsa相机,可以用Sapera软件配置相机,进行图像采集.但是自己开发的话就得撸起袖子写代码了,查了两篇不错的博文,作为指导. Sapera帮助文档 - <好好先生>专栏 - 博 ...

  2. HttpWebRequest 模拟浏览器访问网站

    最近抓网页时报错: 要么返回 The remote server returned an error: (442)要么返回: 非法访问,您的行为已被WAF系统记录! 想了想,就当是人家加了抓网页的东西 ...

  3. Xml 序列化和反序列化

    xml序列化帮助类 using System.IO; using System.Xml; using System.Xml.Serialization; public class XmlHelper ...

  4. [.net]基元线程同步构造

    /* 基元线程同步构造 用户模式构造: 易变构造(Volatile Construct) 互锁构造(Interlocked Construct):自旋锁(Spinlock) 乐观锁(Optimisti ...

  5. c# 字符串中某个词出现的次数及索引

    字符串中某个词出现的次数主要是考察队字符串方法的使用: indexof(): 有9个重载,具体的请转到F12查看详细内容: 本文使用的是第6个重载: 如果找到该字符串,则为从零开始的索引位置:如果未找 ...

  6. 前端工具Rythem介绍

    Rythem是一个与Fiddler同类的软件,和Fiddler一样具有 代理抓包/替换 功能,与Fiddler最大的不同是Rythem是跨平台&开源的. 另外,根据笔者的一次开发经验,Ryth ...

  7. JQuery Mobile - 解决切换页面时,闪屏,白屏等问题

    在点击链接,切换页面时候,总是闪屏,感觉很别扭,看起来不舒服,怎么解决这个问题?方法很简单,就是在每个页面的meta标签内定义user-scalable的属性为 no! <meta name=& ...

  8. Good Bye 2017(送命场)

    9815人数场,9500+围观神仙打架...断断续续打Codeforces也快有一年啦,第一次打Good Bye场,满怀前排膜tourist的心愿参加了这场送命场,虽然没看到tourist.不过还是得 ...

  9. 频繁项集挖掘之Aprior和FPGrowth算法

    频繁项集挖掘的应用多出现于购物篮分析,现介绍两种频繁项集的挖掘算法Aprior和FPGrowth,用以发现购物篮中出现频率较高的购物组合. 基础知识 项:“属性-值”对.比如啤酒2罐.  项集:项的集 ...

  10. Mybatis常用知识点总结

    1. #{}和${}的区别是什么? ${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替换,比如${driver}会被静态替换为com.mysql.jdbc ...