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;
}
}

GridView自动排序相关推荐

  1. GridView 自动排序

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

  2. GridView自动排序(原创)

    前台: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...

  3. gridview的sort_GridView实现自动排序带上下箭头

    GridView排序 ForeColor="#333333" GridLines="None" AllowSorting="True" On ...

  4. vba 自动排序_学会这个Excel表格技巧之后,立刻实现自动排序,太牛了

    怎么实现自动排序呢?老师从网上下载的2018年各大城市最新平均工资排行表 当我改动其中一个城市的平均工资时,比如广州由7965改为10000,整个表格的顺序会自动调整: 是不是很神奇的样子,怎么做到的 ...

  5. python中用def实现自动排序_漫画排序算法Python实现

    冒泡排序 冒泡排序的思想,我们要把相邻的元素两两比较,当一个元素大于右侧相邻元素时, 交换它们的位置;当一个元素小于或等于右侧相邻元素时,位置不变. def bubbleSort(list): ran ...

  6. fastjson 1.2 版本之前的bug, 反序列化时自动排序,导致签名不过

    大家好,我是烤鸭: 今天分享一个问题,使用fastjson 导致签名不过. 1.  问题复现: fastjson 1.2.4 获取返回值: {"data":[{"id&q ...

  7. gridview 实现排序 (在不是使用sqlDataSource控件,而在后台编码绑定gridview时,指定那个字段排序时使用。本例用了单层结构,可修改后应用于多层)

    GridView控件提供了用于实现排序功能的接口,通过设置相关属性并实现排序事件的处理程序就可以完成排序功能.我们将在[例8-4]提供的界面的基础上实现排序功能. [例8-5]演示为GridView控 ...

  8. NetTiers学习笔记09---RADGrid + EntityDataSource设置默认排序, 及表格自动排序,分页的方法...

    前段时间虽然解决了排序和分页的问题,但始终没有解决默认排序的问题, 起初一直把焦点放在EntityDataSource的排序默认值上,但始终搞不定, 今天又花了不少时间Google和研究,终于通过设置 ...

  9. LeetCode-179:数组自动排序工具Arrays.sort(),比较器Comparator的正确打开方式

    题目描述:给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数 实例一:输入: [10,2] 输出: 210 实例二:输入: [3,30,34,5,9] 输出: 9534330 在这道题上花费的 ...

  10. excel设置自动排序123的详细教程

    excel作为主要用于电脑编写的电子表格,主要包括了数据记录和整理.数据的加工和计算.数据的统计和分析等等功能,那么excel怎么设置自动排序123呢?下面小编要讲的内容就是excel设置自动排序教程 ...

最新文章

  1. Solr 使用Facet分组过程中与分词的矛盾解决办法
  2. 基于深度学习的Person Re-ID(特征提取)
  3. ML之ME/LF:基于不同机器学习框架(sklearn/TF)下算法的模型评估指标(损失函数)代码实现及其函数(Scoring/metrics)代码实现(仅代码)
  4. Oracle原理:表分区
  5. Frida之安装和使用教程
  6. Failed connect to github.com:443; No error
  7. GJM:C# WinForm开发系列 - DataGridView 使用方法集锦 [转载]
  8. UI素材渐变配色专辑,对比用色轻松驾驭
  9. ERP项目实施记录08
  10. arm linux 进程页表,arm-linux内存页表创建
  11. vSphere Web Client使用指南之安装配置
  12. mysql5.6免安装版配置
  13. [网络安全自学篇] 八十四.《Windows黑客编程技术详解》之VS环境配置、基础知识及DLL延迟加载详解(1)
  14. [BZOJ4699]树上的最短路(最短路+线段树)
  15. 浅谈CURD系统和CRQS系统
  16. 修改cmd命令窗口的编码格式
  17. NeRF与GAN的交融(一)——NeRF论文阅读
  18. CentOS7防火墙放行端口
  19. C# 将字符串(符合xml格式)与XML互转
  20. 【原创】关于联想Y400适配器和电池同时使用无法开机的问题

热门文章

  1. 延边大学计算机考研压分吗,这6所大学被严重低估,2020届高三考生注意收藏!适合“捡漏”...
  2. Intel XTU 开机自启
  3. 机器学习之SMOTE算法
  4. c语言斐波那契数列_Day 7:斐波那契数列
  5. p6spy oracle,MyBatis集成P6Spy显示实际的SQL(代码教程)
  6. java 手写签名_无纸化办公中手写签名的实现
  7. 联想电脑ideapad_330c拆机清灰、换硅脂步骤大全
  8. 可控硅失效现象_可控硅常见心理问题损坏社会经济现象的介绍和分析
  9. excel文件修复工具_微软修复磁盘清理工具无法完全删除windows.old文件夹问题
  10. matlab分析系统灵敏度,matlab灵敏度分析函数