Telerik Grid Custom Binding:Grouping on Complex Property

2 posts, 0 answers
  1. Lasse
    Lasse avatar
    58 posts
    Member since:
    Oct 2010

    Posted 25 Feb 2014 Link to this post

    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
  2. Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 28 Feb 2014 Link to this post

    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!
Back to Top