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

Telerik Grid Custom Binding:Grouping on Complex Property

1 Answer 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lasse
Top achievements
Rank 1
Lasse asked on 26 Feb 2014, 01:31 AM
i have implemented Custom Binding.I get error like "object reference not set to an instance of an object" when Group a column which is complex Type(Order.Name).

public static IEnumerable<AggregateFunctionsGroup> BuildInnerGroup<T, TObject>(IEnumerable<TObject> group, Func<TObject, T> groupSelector, Func<IEnumerable<TObject>, IEnumerable> innerSelector)
{
return group.GroupBy(groupSelector)
.Select(i => new AggregateFunctionsGroup
{
Key = i.Key,
Items = innerSelector(i)
});
}

public static Func<IEnumerable<TObject>, IEnumerable<AggregateFunctionsGroup>> BuildGroup<T, TObject>(Func<TObject, T> groupSelector, Func<IEnumerable<TObject>, IEnumerable<AggregateFunctionsGroup>> selectorBuilder)
{
var tempSelector = selectorBuilder;
return g => g.GroupBy(groupSelector)
.Select(c => new AggregateFunctionsGroup
{
Key = c.Key,
HasSubgroups = true,
Items = tempSelector.Invoke(c).ToList()
});
}

public static IEnumerable<AggregateFunctionsGroup> ApplyGrouping<T>(this IQueryable<T> data, IList<GroupDescriptor> groupDescriptors)
{
Func<IEnumerable<T>, IEnumerable<AggregateFunctionsGroup>> selector = null;
foreach(var descriptor in groupDescriptors.Reverse())
{
var tempDescriptor = descriptor;
if(selector == null)
selector = g => BuildInnerGroup(g.Select(p => p), p => p.GetType().GetProperty(tempDescriptor.Member).GetValue(p, null), i => i.ToList());
else
selector = BuildGroup(p => p.GetType().GetProperty(tempDescriptor.Member).GetValue(p, null), selector);
}

return selector != null
? selector.Invoke(data).ToList()
: null;
}

For simple field it is working well.When I Group a field with complex type(Order.Name) i get the above error in ApplyGrouping function.In debug mode i see in ApplyGrouping function,the tempDescriptor.Member property value is Order.Name.I need to Split the property on dot(.) and make the grouping(like in the sorting which i already implemented for complex type). Please give a suggestion for grouping.i post the same issue in StackOverflow

http://stackoverflow.com/questions/22023649/telerik-grid-custom-bindinggrouping-on-complex-property

Sabbir

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 28 Feb 2014, 11:06 AM
Hello,

This exception would be thrown if you are using in memory data and some of the related objects are null. You need to check if the object is not null before accessing the property in the selector to avoid the exception in this case. I attached a sample project that demonstrates this scenario.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Lasse
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or