This is a migrated thread and some comments may be shown as answers.

Error on Custom sorting example

2 Answers 30 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kevin Hendriks
Top achievements
Rank 1
Kevin Hendriks asked on 28 Feb 2012, 03:42 PM
Hello,

I have implemented the custom sorting example http://www.telerik.com/help/wpf/gridview-sorting-custom.html 
But using the following line of code results in an error that it cannot sort by System.Object
employees = employees.OrderByDescending( employee => employee.GetType()
                                                            .GetProperty( (e.Column as GridViewDataColumn).GetDataMemberName() )
                                                            .GetValue( employee, null ) );
So I think the example code is not working, but I do not seem to be able to figure out how to get it working.
I am using this code on the server side so the code looks like this.

IQueryable<Customer> custList = dbProPlan.Customers.Where(cust => !cust.IsDeleted);
                if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Ascending)
                {
                    custList = custList.OrderBy(cust => cust.GetType().GetProperty(propertyName).GetValue(cust, null));
                }
                else if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Ascending)
                {
                    custList = custList.OrderByDescending(cust => cust.GetType().GetProperty(propertyName).GetValue(cust, null));
                } 


                numberOfItems = custList.Count();
                double pages = numberOfItems / aPageSize;
                int numberOfPages = (Math.Ceiling(pages) >= 1 ? (int)Math.Ceiling(pages) : 1);
                aPageNumber = (numberOfPages < aPageNumber ? numberOfPages - 1 : aPageNumber);


                IEnumerable<Customer> customerList = custList.Skip(aPageNumber * aPageSize).Take(aPageSize);
                dbProPlan.Refresh(RefreshMode.OverwriteCurrentValues, customerList);
                return customerList;

What do I need to change to get this working properly.

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 02 Mar 2012, 01:38 PM
Hello,

 Can you post the stack trace? The grid version will be helpful as well. 

Greetings,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Kevin Hendriks
Top achievements
Rank 1
answered on 09 Mar 2012, 02:27 PM
I have solved the orror by using the following code
public static Expression<Func<T, object>> OrderExpression<T>(string propertyName)
        {
            // First we define the parameter that we are going to use in our OrderBy clause. This is the same as "(person =>"
            var param = Expression.Parameter(typeof(T), "item");
 
            // Now we'll make our lambda function that returns the property by it's name.
            var mySortExpression = Expression.Lambda<Func<T, object>>(Expression.Property(param, propertyName), param);
            return mySortExpression;
        }
 
public IEnumerable<Customer> GetAllCustomers(int aPageNumber, int aPageSize, out int numberOfItems, string propertyName, SortingState sortingState)
        {
            numberOfItems = 0;
            try
            {
                IQueryable<Customer> query = dbProPlan.Customers.Where(cust => !cust.IsDeleted);
                if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Ascending)
                {
                    query = query.OrderBy(OrderExpression<Customer>(propertyName));
                }
                else if (!string.IsNullOrEmpty(propertyName) && sortingState == SortingState.Descending)
                {
                    query = query.OrderByDescending(OrderExpression<Customer>(propertyName));
                }
                numberOfItems = query.Count();
                double pages = numberOfItems / aPageSize;
                int numberOfPages = (Math.Ceiling(pages) >= 1 ? (int)Math.Ceiling(pages) : 1);
                aPageNumber = (numberOfPages < aPageNumber ? numberOfPages - 1 : aPageNumber);
 
                IEnumerable<Customer> customerList = query.Skip(aPageNumber * aPageSize).Take(aPageSize);
                dbProPlan.Refresh(RefreshMode.OverwriteCurrentValues, customerList);
                return customerList;
            }
            catch (Exception e)
            {
                LogWriter.WriteLine(LogTypes.Error, string.Format("GetAllCustomers() failed; {0}", e.Message), e.StackTrace);
                return null;
            }
        }
Tags
GridView
Asked by
Kevin Hendriks
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Kevin Hendriks
Top achievements
Rank 1
Share this question
or