开发者

Sorting Problem using Custom Entity Databinding

开发者 https://www.devze.com 2023-02-26 22:56 出处:网络
I have problem sorting my Gridview using my custom entity classes My complex entity public class Subscription

I have problem sorting my Gridview using my custom entity classes

My complex entity

public class Subscription
{

    public string SubscriptionID { get; set; }
    public s开发者_StackOverflow中文版tring Status { get; set; }
    public Customer { get; set; } //Contained class from another entity
}


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

Binding to gridview

 List<Subscription> subscriptions = GetSubscriptions();


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



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

I use template field to bind my entity fields into the grid

         <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>

Now in sorting, its works fine when sorting Subscription's Native property

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

But I'm having problem in sorting properties of the contained class (Customer)

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

I tried SortExpression="Customer.CustomerName" but to no avail

I used this GenericComparer in sorting the grid

   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);
    }
}

additional details:

     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;

    }
}

Please advise, thanks in advance


One quick-and-dirty thing you could try is to add a CustomerName property to your Subscription class. I usually do this in a client-side code partial class when using Silverlight/WCF/EF.

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

Then reference this property from your UI.

0

精彩评论

暂无评论...
验证码 换一张
取 消