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

Grid sorting on custom collections

3 Answers 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Martin Dufour
Top achievements
Rank 1
Martin Dufour asked on 24 Jan 2012, 10:00 PM
Hi,

When binding the grid to a List<CutomObject>, it throws an exception (Object Reference Not Set) in System.Linq.EnumerableSorter`2.ComputeKeys.

I already read other threads about that, it seems that setting EnableLinqExpression to false should do the trick.
It doesn't work for me.

I use a LinqDataSource to bind the grid.

Please help, it seems so obvious to me that this should work!

Patrick

3 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 27 Jan 2012, 10:53 AM
Hello Patrick,

I tried to replicate the issue which you described, but to no avail. Attached to this message, you will find the code which I used for testing.

Please, take a look at it and let me know if there are any differences at your end, which I may be leaving out.

Greetings,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Martin Dufour
Top achievements
Rank 1
answered on 31 Jan 2012, 03:38 PM

Hi,

I forgot to mention an important thing, sorting doesn't work if CutomObject has nested object properties that can be null.
For example, if the column has a SortExpression = "ParentCustomObject.Name" and that ParentCustomObject is null, the exception is thrown.

Anyway, I found a solution for that problem.  For the above example to work, the SortExpression should be:
ParentCustomObject != null ? ParentCustomObject.Name : ""


If fact, the linq framework parse this code so at runtime it works perfect.

Here's the code I use to generate a proper SortExpression:

          

string dataField = GetDataField();
  
//For each '.' we found, we check null value
string[] split = dataField.Split('.');
if (split.Length == 1)
    return dataField;
  
//Ex1: "A != null ? A.B : \"\"";
//Ex2: "A != null && A.B != null ? A.B.C : \"\"";
StringBuilder sbObjectToCheck = new StringBuilder();
StringBuilder sbSortExpression = new StringBuilder();
for (int i = 0; i < split.Length - 1; i++)
{
    if (i > 0)
    {
        sbObjectToCheck.Append(".");
        sbSortExpression.Append(" && ");
    }
  
    sbObjectToCheck.Append(split[i]);
    sbExpression.AppendFormat("{0} != null", sbObjectToCheck.ToString());
}
  
string sortExpression = string.Format("{0} ? {1} : \"\"", sbExpression.ToString(), dataField);
return sortExpression;

Maybe you should add this workaround in a future release.

Thank you for your quick support !

0
Mira
Telerik team
answered on 01 Feb 2012, 03:37 PM
Hello Patrick,

I am glad that the issue is resolved.

Thank you for sharing the solution with the community.

All the best,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Martin Dufour
Top achievements
Rank 1
Answers by
Mira
Telerik team
Martin Dufour
Top achievements
Rank 1
Share this question
or