1、效果图

2、前台代码

说明:红色代码为核心代码

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False"
OnRowDataBound="gvData_RowDataBound"
onsorting="gvData_Sorting" AllowSorting="true"
>
<Columns>
<asp:TemplateField HeaderText="新闻标题">
<ItemTemplate>
<asp:Label ID="labtitle" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="阅读量" SortExpression="count">
<ItemTemplate>
<asp:Label ID="labcount" runat="server" Text='<%# Bind("count") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="添加人">
<ItemTemplate>
<asp:Label ID="labadmin" runat="server" Text='<%# Bind("adminName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="25%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="时间" SortExpression="ID">
<ItemTemplate>
添加:<asp:Label ID="labtime" runat="server" Text='<%# Bind("times") %>'></asp:Label>
<asp:Label ID="labtimepass" runat="server" Text='<%# Bind("timepass") %>'></asp:Label>
<br />
</ItemTemplate>
<ItemStyle Width="30%" />
</asp:TemplateField>
</Columns>
</asp:GridView>

3、后台代码

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortOrder"] = "ID";  //默认排序字段,必须
ViewState["OrderDire"] = "Desc"; //默认排序,必须
fillData();
}
}
protected void fillData()
{
    string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
    string sql = “select * from dt_news ”+sort;
    
    //datatable 自己写,绑定GridView自己写,我这里就不写了
  }
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断是否是表头
if (e.Row.RowType == DataControlRowType.Header)
{
//判断是否进行排序
//是否是排序字段信息
for (int i = ; i < e.Row.Cells.Count; i++)
{
ControlCollection cons= e.Row.Cells[i].Controls;
if (cons.Count == )
{
LinkButton lb = cons[] as LinkButton;
if (lb != null)
{
if (ViewState["SortOrder"].ToString() == lb.CommandArgument)
{
if (ViewState["OrderDire"].ToString() == "Desc")
{
lb.Text += "▼";
}
else
{
lb.Text += "▲";
}
}
}
}
}
}
}
protected void gvData_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;
}
PageControl1.pageNow = ;
fillData();
}

GridView表头排序方法设置的更多相关文章

  1. DBGridEh 点击表头排序方法

    方法1: (不用编程写代码) 程序中引用 单元 EhLibCDS设置DBGridEh的属性:      ColumnDefValues.Title.TitleButton = True      Op ...

  2. CList 点击表头排序 (2)两种排序方法中其中一种

    上一篇讲解SortItem()方法如何使用,虽然都是抄别人的但是就是想让大家有个大概的了解 CList 点击表头排序 (1)SortItems函数 点击表头排序基本思路都是 1.首先响应HDN_ITE ...

  3. gridview自定义排序

    效果如图: 首先允许排序:AllowSorting="True":开启gridview的排序事件onsorting="GridView1_Sorting",也可 ...

  4. CList 点击表头排序 (1)SortItems函数

    点击表头排序整体的思路都是去 CListCtrl类中的方法SortItems去实现 CListCtrl::SortItems的原型是: BOOL SortItems( PFNLVCOMPARE pfn ...

  5. JavaScript高级程序设计--对象,数组(栈方法,队列方法,重排序方法,迭代方法)

    1.使用对象字面量定义对象 var person={}; 使用这种方式创建对象时,实际上不会调用Object构造函数. 开发人员更喜欢对象字面量的语法.   2.有时候需要传递大量可选参数的情形时,一 ...

  6. php语言实现的7种基本的排序方法

    今天总结了一下常用的7种排序方法,并用php语言实现. 直接插入排序 /* * 直接插入排序,插入排序的思想是:当前插入位置之前的元素有序, * 若插入当前位置的元素比有序元素最后一个元素大,则什么也 ...

  7. javascript: 带分组数据的Table表头排序

    如下图: 要求:点击表头排序时,"分组"及"分组明细"的数据层次关系不变 从网上找了一段常规的table排序,改了改,以满足“分组支持”,贴在这里备份 < ...

  8. lucene之排序、设置权重、优化、分布式搜索(转)

    lucene之排序.设置权重.优化.分布式搜索(转) 1. 基本应用 using System;using System.Collections.Generic;using System.Text;u ...

  9. ## GridView 布局:item设置的高度和宽度不起作用、自动适配列数、添加Header和Footer ##

    一.item设置的高度和宽度不起作用 转自:http://www.cnblogs.com/0616--ataozhijia/p/6031875.html [Android Pro] listView和 ...

随机推荐

  1. ProgressDialog弹出时的底色变暗(转)

    背景:在做一个进度条时,不想让它的背景变暗,以免影响其他区域的正常显示. 在网上搜索时,看到的方法多数是: 方法一 :在代码中 可以这么设置 Window mWindow = getWindow(); ...

  2. mysql数据恢复

    [1] 当数据库被删除后的恢复方法   首先建立一个测试用的数据库.  mysql -u root -p123123   ← 用root登录到MySQL服务器  Enter password:  ←  ...

  3. d3d11 effect state and default value tables

    Blend state State Default ValueAlphaToCoverage Enable FALSEIndependentBlend Enable FALSERenderTarget ...

  4. 国内一些SCM相关论坛站点

    SCMROAD: http://www.scmroad.com/forum.php SCMEYE:http://www.scmeye.com/ SVN管家:http://www.svnclub.com ...

  5. lunux 启动 tomcat

    本人从官网http://tomcat.apache.org/上面下载的6.0.1_31版本,并解压包后改名存放在:/usr/share/tomcat6 本人使用的是root用户登录,下面就说说具体的操 ...

  6. Ubuntu 12.04下虚拟磁带库mhvtl的安装和使用

      项目需要连接一下昆腾虚拟磁带库DXI 6701 ,这玩意太贵,不好得到,先弄个虚拟软件测试了, 网上了一下,有这个软件: mhvtl   主页: https://sites.google.com/ ...

  7. POJ 1663

    #include<iostream>//cheng da cai zi using namespace std; int main() { int time; cin>>tim ...

  8. 移动开发之meta篇

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable= ...

  9. Android 使用系统的Activity播放音频文件 intent

    Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Inten ...

  10. 深入理解JVM—Java 6 JVM参数配置说明

    原文地址:http://yhjhappy234.blog.163.com/blog/static/316328322011119111014657/ 使用说明< xmlnamespace pre ...