• GirdView学习之三排序


    要先设置GridView的AllowSortring=true,这样当点击列标题的时候才能激发GridView的Sorting事件进行排序

     1using System;
     2using System.Data;
     3using System.Configuration;
     4using System.Collections;
     5using System.Web;
     6using System.Web.Security;
     7using System.Web.UI;
     8using System.Web.UI.WebControls;
     9using System.Web.UI.WebControls.WebParts;
    10using System.Web.UI.HtmlControls;
    11using System.Collections.Generic;
    12
    13public partial class GridViewSortingTest : System.Web.UI.Page
    14{
    15    protected void Page_Load(object sender, EventArgs e)
    16    {
    17        if (!IsPostBack)
    18        {
    19            ClientInfoAccessObj accessor = new ClientInfoAccessObj();
    20            GridView1.DataSource = accessor.GetAllClients();//绑定所有客户信息
    21            GridView1.DataBind();
    22        }

    23    }

    24    //按照客户姓名进行排序比较
    25    public int CompareByClientName(ClientInfo Client1, ClientInfo Client2)
    26    {
    27        return Client1.ClientName.CompareTo(Client2.ClientName);
    28    }

    29
    30    //按照邮编和地址进行排序比较
    31    public int CompareByPostCodeAndAddressStr(ClientInfo client1, ClientInfo client2)
    32    {
    33        int ret = client1.PostCode.CompareTo(client2.PostCode);
    34        if (ret != 0)
    35            return ret;
    36        else//如果邮编一样
    37        {
    38            return client1.AddressStr.CompareTo(client2.AddressStr);
    39        }

    40    }

    41    //按照邮编进行排序比较
    42    public int CompareByPostCode(ClientInfo client1, ClientInfo client2)
    43    {
    44        return client1.PostCode.CompareTo(client2.PostCode);
    45    }

    46    //正在排序的事件
    47    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    48    {
    49        ClientInfoAccessObj accessor = new ClientInfoAccessObj();
    50        List<ClientInfo> clients = accessor.GetAllClients();
    51        switch (e.SortExpression)
    52        {
    53            case "ClientName":
    54                clients.Sort(CompareByClientName);//参数是一个Comparison<T>类型的泛型委托的函数名
    55                break;
    56            case "MultiColumnSort":
    57                clients.Sort(CompareByPostCodeAndAddressStr);
    58                break;
    59            case "PostCode":
    60                clients.Sort(CompareByPostCode);
    61                break;
    62            default:
    63                ClientScript.RegisterClientScriptBlock(this.GetType(), "InfoMsg""alert('不支持对此字段进行排序');"true);
    64                break;
    65        }

    66        GridView1.DataSource = clients;//绑定显示数据
    67        GridView1.DataBind();
    68    }

    69    protected void btnSortByName_Click(object sender, EventArgs e)
    70    {
    71        GridView1.Sort("ClientName", SortDirection.Ascending);//此事件执行完毕再执行Sorting事件
    72    }

    73    protected void btnSortByPostCodeAndAddress_Click(object sender, EventArgs e)
    74    {
    75        GridView1.Sort("MultiColumnSort", SortDirection.Ascending);
    76    }

    77}

    78
  • 相关阅读:
    tomcat调试页面的时候,不刷新
    $.ajax()方法详解(转)
    Zookeeper简述
    简述Dubbo
    Nginx入门
    Redis入门
    JVM入门
    spring MVC框架入门(外加SSM整合)
    Mybatis框架入门
    Spring+Hibernate+Struts(SSH)框架整合
  • 原文地址:https://www.cnblogs.com/mdy41034264/p/1356166.html
Copyright © 2020-2023  润新知