惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Heimdal Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
V
V2EX
WordPress大学
WordPress大学
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
AI
AI
小众软件
小众软件
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
B
Blog RSS Feed
Forbes - Security
Forbes - Security
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Project Zero
Project Zero
H
Hacker News: Front Page
Y
Y Combinator Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 网际浪人

.Net Framework 4.0 中利用Task实现并行处理、串并行混合处理 C# Process调用应用程序失败时应注意的问题 程序员的幽默 To腾讯:强行收集用户个人隐私的行为不可饶恕 VS2005打开VS2008项目的2种方法(转) 【转】Oracle Conversion Functions 晨星、银河基金业绩排行榜数据转换工具 ORACLE纯SQL实现多行合并一行 ASP.NET项目添加Log4Net后,发布后无法写日志 “必应”不应、“谷歌”不歌 你好2009,再见2008,牛年犇犇犇 一种在SQLServer中实现Sequence的高效方法 oledb使用Access更新和插入操作的注意点 5.12大地震——小学二年级小表妹谢可欣的诗 [转]SQL Server 2005链接字符串 对HtmlEncode的增强——HtmlEntitiesEncode GridView中使用DataKeyNames存储数据键值 C#调用Excel VBA宏 封装SoapException处理Webservice异常
GridView自动排序
网际浪人 · 2008-06-02 · via 博客园 - 网际浪人

GridView自带了数据排序功能。在设计视图下,只能对GridView的排序数据列和排序方向进行静态设置。在后台程序中,则需要用Attributes方式对GridView的这两个属性进行动态设置。

示例如下:
(前台)

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">
            
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            
<RowStyle BackColor="#EFF3FB" />
            
<Columns>
                
<asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
                
<asp:BoundField DataField="name" HeaderText="NAME" SortExpression="name" />
                
<asp:BoundField DataField="age" HeaderText="AGE" SortExpression="age" />
            
</Columns>
            
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            
<EditRowStyle BackColor="#2461BF" />
            
<AlternatingRowStyle BackColor="White" />
        
</asp:GridView>
    
</div>
    
</form>
</body>
</html>

前台注意点:
需要对GridView启用AllowSorting、设置OnSorting事件,对需要排序的列设定SortExpression属性。

(后台)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            
// 设定初始排序参数值// 错误的属性设置方法:SortExpression、SortDirection均是GridView只读属性,无法直接赋值。
            
//this.GridView1.SortExpression = "id";
            
//this.GridView1.SortDirection = "ASC";// 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression""id");
            
this.GridView1.Attributes.Add("SortDirection""ASC");// 绑定数据源到GridView
            this.BindGridView();
        }
    }
/// <summary>
    
/// GridView排序事件
    
/// </summary>
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        
// 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();// 假定为排序方向为“顺序”
        string sortDirection = "ASC";// “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            
//获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }
// 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"= sortExpression;
        
this.GridView1.Attributes["SortDirection"= sortDirection;this.BindGridView();
    }
/// <summary>
    
/// 绑定到GridView
    
/// </summary>
    private void BindGridView()
    {
        
// 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        
string sortDirection = this.GridView1.Attributes["SortDirection"];// 调用业务数据获取方法
        DataTable dtBind = this.getDB();// 根据GridView排序数据列及排序方向设置显示的默认数据视图
        if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
        {
            dtBind.DefaultView.Sort 
= string.Format("{0} {1}", sortExpression, sortDirection);
        }
// GridView绑定并显示数据
        this.GridView1.DataSource = dtBind;
        
this.GridView1.DataBind();
    }
/// <summary>
    
/// 获取数据源的方法
    
/// </summary>
    
/// <returns>数据源</returns>
    private DataTable getDB()
    {
        DataTable dt 
= new DataTable();

        dt.Columns.Add(

"id");
        dt.Columns.Add(
"name");
        dt.Columns.Add(
"age");

        dt.Rows.Add(

new object[] { "000001""hekui""26" });
        dt.Rows.Add(
new object[] { "000002""zhangyu""26" });
        dt.Rows.Add(
new object[] { "000003""zhukundian""27" });
        dt.Rows.Add(
new object[] { "000004""liyang""25" });
        dt.Rows.Add(
new object[] { "000005""caili""27" });return dt;
    }
}

示例工程:
/Files/heekui/GridViewSort.rar