GridView表头排序方法设置
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表头排序方法设置的更多相关文章
- DBGridEh 点击表头排序方法
方法1: (不用编程写代码) 程序中引用 单元 EhLibCDS设置DBGridEh的属性: ColumnDefValues.Title.TitleButton = True Op ...
- CList 点击表头排序 (2)两种排序方法中其中一种
上一篇讲解SortItem()方法如何使用,虽然都是抄别人的但是就是想让大家有个大概的了解 CList 点击表头排序 (1)SortItems函数 点击表头排序基本思路都是 1.首先响应HDN_ITE ...
- gridview自定义排序
效果如图: 首先允许排序:AllowSorting="True":开启gridview的排序事件onsorting="GridView1_Sorting",也可 ...
- CList 点击表头排序 (1)SortItems函数
点击表头排序整体的思路都是去 CListCtrl类中的方法SortItems去实现 CListCtrl::SortItems的原型是: BOOL SortItems( PFNLVCOMPARE pfn ...
- JavaScript高级程序设计--对象,数组(栈方法,队列方法,重排序方法,迭代方法)
1.使用对象字面量定义对象 var person={}; 使用这种方式创建对象时,实际上不会调用Object构造函数. 开发人员更喜欢对象字面量的语法. 2.有时候需要传递大量可选参数的情形时,一 ...
- php语言实现的7种基本的排序方法
今天总结了一下常用的7种排序方法,并用php语言实现. 直接插入排序 /* * 直接插入排序,插入排序的思想是:当前插入位置之前的元素有序, * 若插入当前位置的元素比有序元素最后一个元素大,则什么也 ...
- javascript: 带分组数据的Table表头排序
如下图: 要求:点击表头排序时,"分组"及"分组明细"的数据层次关系不变 从网上找了一段常规的table排序,改了改,以满足“分组支持”,贴在这里备份 < ...
- lucene之排序、设置权重、优化、分布式搜索(转)
lucene之排序.设置权重.优化.分布式搜索(转) 1. 基本应用 using System;using System.Collections.Generic;using System.Text;u ...
- ## GridView 布局:item设置的高度和宽度不起作用、自动适配列数、添加Header和Footer ##
一.item设置的高度和宽度不起作用 转自:http://www.cnblogs.com/0616--ataozhijia/p/6031875.html [Android Pro] listView和 ...
随机推荐
- Android开发应用异步检查更新代码
开发环境:android studio sdk 4.0及以上 场景:用户点击检查更新按钮进行检查服务器版本号,若有新版本则进行下载更新.异步检测版本号 package com.example.q ...
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能
MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQ ...
- Codeforces Round #327 (Div. 1) B. Chip 'n Dale Rescue Rangers 二分
题目链接: 题目 B. Chip 'n Dale Rescue Rangers time limit per test:1 second memory limit per test:256 megab ...
- JS中的className含义
问题描述: JS中的className含义 问题解决: className说明: className属性可以设置和返回元素的class属性 可以有两种方法来获取对象的c ...
- OWASP
开放式Web应用程序安全项目(OWASP,Open Web Application Security Project)是一个组织,它提供有关计算机和互联网应用程序的公正.实际.有成本效益的信息.其目的 ...
- Ignore files which are already versioned
If you accidentally added some files which should have been ignored, how do you get them out of vers ...
- ECMALL目录结构设置与数据库表
[Ecmall]ECMALL目录结构设置与数据库表 最近在做ecmall的开发,ecmall在开源方面还有待进步啊,官方没有提供开发文档,也没有关于系统架构组织的贡献,使用者都要自己从0开始,官方 ...
- Winform 窗体的操作
原文:http://www.cnblogs.com/Billy-rao/archive/2012/05/16/2503437.html 怎样能使winform窗体的大小固定住,不能调整其大小 窗体Fo ...
- What is the Best Programming Language to Learn in 2014?
It’s been a year since I revealed the best languages to learn in 2013. Once again, I’ve examined the ...
- [转] 浅析HTTP协议
浅析HTTP协议 来源:http://www.cnblogs.com/gpcuster/archive/2009/05/25/1488749.html HTTP协议是什么? 简单来说,就是一个基于应用 ...