zoukankan      html  css  js  c++  java
  • ObjectDataSourc用法之三(排序)

     

    ObjectDataSourc用法之三(排序)

    SortParameterName參數主要用於對數據源控件進尾排序

    1.       准備條件

    參數:ObjectDataSource用法之一(SelectMethod來進行簡單的邦定)

    添加一個處理對象排序的類Reverser

    public class Reverser<T> : IComparer<T>

    {

        private Type type = null;

        private ReverserInfo info;

        public Reverser(string className, string name, ReverserInfo.Direction direction)

        {

            try

            {

                this.type = Type.GetType(className, true);

                this.info.name = name;

                this.info.direction = direction;

            }

            catch (Exception e)

            {

                throw new Exception(e.Message);

            }

        }

        int IComparer<T>.Compare(T t1, T t2)

        {

            object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);

            object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);

            if (this.info.direction != ReverserInfo.Direction.ASC)

                Swap(ref x, ref y);

            return (new CaseInsensitiveComparer()).Compare(x, y);

        }

        private void Swap(ref object x, ref object y)

        {

            object temp = null;

            temp = x;

            x = y;

            y = temp;

        }

    }

     

    public struct ReverserInfo

    {

        public enum Direction

        {

            ASC = 0,

            DESC,

        };

        public enum Target

        {

            CUSTOMER = 0,

            FORM,

            FIELD,

            SERVER,

        };

        public string name;

        public Direction direction;

        public Target target;

    }

    2.       在業務處理類中添加如下方法

    public List<EntityMember> OrderItems(string order)

    {

        string orderName = order.Split(' ')[0];

        ReverserInfo.Direction dir = ReverserInfo.Direction.ASC;

        if (order.Split(' ').Length>1 && order.Split(' ')[1] == "DESC") dir = ReverserInfo.Direction.DESC;

        List<EntityMember> result = new List<EntityMember>();

        XmlDocument doc = new XmlDocument();

        doc.Load(_path);

        XmlNodeList nodes = doc.SelectNodes("/Members/Member");

        foreach (XmlNode node in nodes)

        {

            result.Add(new EntityMember(node.SelectSingleNode("./UID").InnerText, node.SelectSingleNode("./PWD").InnerText, node.SelectSingleNode("./Email").InnerText));

        }

        Reverser<EntityMember> reverser = new Reverser<EntityMember>("EntityMember", orderName, dir);

        result.Sort(reverser);

        return result;

     

    說明:當按降序排列的時候,參數order的內容為:屬性名稱+空格+DESC

           當按升序排列的時候,參數order的內容為:屬性名稱

    3.       Aspx頁面的內容為

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"

        SelectMethod="OrderItems" SortParameterName="order" TypeName="Member"></asp:ObjectDataSource>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

        DataSourceID="ObjectDataSource1" AllowSorting="true">

        <Columns>

            <asp:BoundField DataField="UID" HeaderText="UID" SortExpression="UID" />

            <asp:BoundField DataField="PWD" HeaderText="PWD" SortExpression="PWD" />

            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />

        </Columns>

    </asp:GridView>

    說明:SortParameterName為指定SelectMethod參數指定的方法中用於排序的參數名稱

     

  • 相关阅读:
    ssh-copy-id 的使用方法
    如何保证 docker daemon重启,但容器不重启
    vim设置golang语法高亮 (Centos)
    Error response from daemon: Error running DeviceCreate (createSnapDevice) dm_task_run failed
    Please supply the message using either -m or -F option.
    sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix
    vim plugins (vim 插件) 工具集
    OmniGraffle v6 注册码
    test
    Collections.addAll 为什么比collection.addall 快(转)
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/1567720.html
Copyright © 2011-2022 走看看