使用自定义实体数据绑定排序问题

本文关键字:排序 问题 数据绑定 实体 自定义 | 更新日期: 2023-09-27 17:50:32

我有问题排序我的Gridview使用我的自定义实体类

我的复杂实体

public class Subscription
{
    public string SubscriptionID { get; set; }
    public string Status { get; set; }
    public Customer { get; set; } //Contained class from another entity
}

public class Customer
{
    public string CustomerID { get; set; }
    public CustomerName { get; set; }
}

绑定gridview

 List<Subscription> subscriptions = GetSubscriptions();

 //sorting, sortEvent is the GridviewSortEvenArgs param
 subscriptions.Sort(new GenericComparer<Subscription>(sortEvent.SortExpression, GridViewSortDirection));

 grdSubscription.DataSource = subscriptions;
 grdSubscription.DataBind();

我使用模板字段绑定我的实体字段到网格

         <asp:TemplateField HeaderText="Subscription ID" SortExpression="SubscriptionID">
                    <ItemTemplate>
                       <%# Eval(" SubscriptionID ") %>
                    </ItemTemplate>
                </asp:TemplateField>
       <asp:TemplateField HeaderText="Customer Name" SortExpression="CustomerName">
                    <ItemTemplate>
                       <%# Eval(" Customer.CustomerName ") %>
                    </ItemTemplate>
                </asp:TemplateField>

在排序中,它可以很好地排序Subscription的Native属性

     <asp:TemplateField HeaderText="Subscription ID" SortExpression="SubscriptionID">

但是我在包含类(Customer)的排序属性方面有问题

       <asp:TemplateField HeaderText="Customer Name" SortExpression="CustomerName">
                <ItemTemplate>
                   <%# Eval(" Customer.CustomerName ") %>
                </ItemTemplate>

我试过SortExpression="Customer. "CustomerName"但无济于事

我使用了这个GenericComparer来排序网格

   public class GenericComparer<T> : IComparer<T>
{
    private SortDirection sortDirection;
    public SortDirection SortDirection
    {
        get { return this.sortDirection; }
        set { this.sortDirection = value; }
    }
    private string sortExpression;
    public GenericComparer(string sortExpression, SortDirection sortDirection)
    {
        this.sortExpression = sortExpression;
        this.sortDirection = sortDirection;
    }
    public int Compare(T x, T y)
    {
        PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression);
        IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
        IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);
        if (SortDirection == SortDirection.Ascending)
        {
            return obj1.CompareTo(obj2);
        }
        else return obj2.CompareTo(obj1);
    }
}

额外的细节:

     protected SortDirection GridViewSortDirection
{
    get
    {
        // Checks for the first time when the ViewState sort direction is null
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        // Changes the sort direction
        else
        {
            if (((SortDirection)ViewState["sortDirection"]) == SortDirection.Ascending)
            {
                ViewState["sortDirection"] = SortDirection.Descending;
            }
            else
            {
                ViewState["sortDirection"] = SortDirection.Ascending;
            }
        }
        return (SortDirection)ViewState["sortDirection"];
    }
    set
    {
        ViewState["sortDirection"] = value;
    }
}

请告知,谢谢提前

使用自定义实体数据绑定排序问题

您可以尝试的一种快速而肮脏的方法是向Subscription类添加CustomerName属性。当使用Silverlight/WCF/EF时,我通常在客户端代码部分类中这样做。

public string CustomerName 
{ get 
   { return Customer != null ? Customer.CustomerName : string.empty }
}