0

I have a grid where I am biding data from an object. Object has some complex data types. Below is my code Able to sort ORDERID and OrderNumber BUT NOT CustomerInfo Property

Class :

public class Order
{    
    public Int32 OrderID{ get; set; }
    public String OrderNumber { get; set; }        
    public Customer CustomerInfo { get; set;}
}

public class Customer
{
    public Int64 CustomerNumber { get; set; }
    public String FirstName { get; set; }
    public String  LastName { get; set; }
    public String Phone { get; set; }
}

My Front End grid has columns defined one of the columns is

<asp:TemplateField HeaderText="Last Name"  ItemStyle-Width="15%" SortExpression="OrderCustomerInfo.LastName">
    <ItemTemplate>
        <%# Eval("OrderCustomerInfo.LastName") %>
    </ItemTemplate>
</asp:TemplateField>

When I am passing Sort Expression : OrderCustomerInfo.LastName it's not recognizing the column below is my sort function, I am getting error --

Instance property 'OrderCustomerInfo.LastName' is not defined for type 'Order' Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Instance property 'OrderCustomerInfo.LastName' is not defined for type 'Order' )

Error Line -- Expression property = Expression.Property(param, columnName); // x.ColumnName

public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> collection, string columnName, SortDirection direction)
{
    ParameterExpression param = Expression.Parameter(typeof(T), "x"); // x
    Expression property = Expression.Property(param, columnName);     // x.ColumnName
    Func<T, object> func = Expression.Lambda<Func<T, object>>(        // x => x.ColumnName
    Expression.Convert(Expression.Property(param, columnName),
    typeof(object)), param).Compile();

    Func<IEnumerable<T>, Func<T, object>, IEnumerable<T>> expression =
        SortExpressionBuilder<T>.CreateExpression(direction);
    IEnumerable<T> sorted = expression(collection, func);

    return sorted;
}
msbyuva
  • 3,189
  • 12
  • 57
  • 84

2 Answers2

0

Is the issue not that this line:

<%# Eval("OrderCustomerInfo.LastName") %>

Is missing a .?

<%# Eval("Order.CustomerInfo.LastName") %>
Vae
  • 91
  • 2
  • 6
  • No,I am binding the object returned from data access which will use those classes... object name is OrderCustomerInfo and the property needs to be binded is LastName so it's OrderCustomerInfo.LastName – msbyuva Jun 10 '14 at 16:00
  • Does changing it to CustomerInfo.LastName work? It sounds to me as if OrderCustomerInfo is a List. I'm a work so I can't try this out, but I have a feeling your function will also need to be modified to work with complex objects. At a glance it doesn't look like it will handle nested properties – Vae Jun 10 '14 at 16:09
  • no it didn't work.... it works fine for regular properties but not for custom properties.. – msbyuva Jun 10 '14 at 16:17
0

The exception states that:

Exception Details: System.ArgumentException: Instance property 'OrderCustomerInfo.LastName' is not defined for type 'Order' )

So I'd reckon you need to update your SortExpression and Eval as:

<asp:TemplateField HeaderText="Last Name"  ItemStyle-Width="15%" SortExpression="CustomerInfo.LastName">
    <ItemTemplate>
        <%# Eval("CustomerInfo.LastName") %>
    </ItemTemplate>
</asp:TemplateField>
rageit
  • 3,320
  • 1
  • 24
  • 37
  • I updated as mentioned in my previous commnets.... it didn't work .. May be I need to tweak the OrderBy fucntion to support complext types.. – msbyuva Jun 10 '14 at 17:23