This question is locked. New answers and comments are not allowed.
|
Requirements |
|
| RadControls version | 2011.1.315 |
| .NET version | 4.0 |
| Visual Studio version | VS 2010 |
| programming language | C# |
| browser support |
all browsers supported by RadControls |
PROJECT DESCRIPTION
These helper methods utilize generics and reflection in order to simplify grouping when using custom binding. These are based on other examples provided by Telerik, but simplify usage by allowing you to avoid a series of if/then or switch statements based on the grouped field. The helper class is as follows:
public static class TelerikGridHelpers{ 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>(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; }}In order to utilize these helpers in a custom binding scenario, you must first ensure that your data source first applies sorting by the fields (in reverse) in the GridCommand.GroupDescriptors argument to the controller action you're calling to get the GridModel.
Once you have your raw IEnumerable of the grid items, you simply need to return the GridModel as follows:
return command.GroupDescriptors.Any() ? new GridModel { Data = TelerikGridHelpers.ApplyGrouping(gridItems.AsQueryable(), command.GroupDescriptors), // In my instance this is from my call to an NHibernate repository because I'm doing paging on the DB. Total = (Int32) total.Value } : new GridModel {Data = accountDTOs, Total = (Int32) total.Value};This saves having to put "magic string" tests for the Member value of the group descriptor.