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參數指定的方法中用於排序的參數名稱

 

原文地址:https://www.cnblogs.com/OpenCoder/p/1567720.html