ObjectDataSourc用法之三(排序)
?
ObjectDataSourc用法之三(排序)
SortParameterName參數(shù)主要用於對數(shù)據(jù)源控件進(jìn)尾排序
1.?????? 準(zhǔn)備條件
參數(shù):ObjectDataSource用法之一(SelectMethod來進(jìn)行簡單的邦定)
添加一個處理對象排序的類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.?????? 在業(yè)務(wù)處理類中添加如下方法
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;
?
說明:當(dāng)按降序排列的時候,參數(shù)order的內(nèi)容為:屬性名稱+空格+DESC
?????? 當(dāng)按升序排列的時候,參數(shù)order的內(nèi)容為:屬性名稱
3.?????? Aspx頁面的內(nèi)容為
<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參數(shù)指定的方法中用於排序的參數(shù)名稱
?
總結(jié)
以上是生活随笔為你收集整理的ObjectDataSourc用法之三(排序)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OMNet++ 4.0
- 下一篇: 找到了 。。。