GridDataBoundGrid, sorting custom collection

Hi,
Does anybody have an example of custom collection sorting in GDBG?

I know that the grid is based on DataView sorting, but there were some examples of how to achieve this, unfortunately the links are broken, for example see

http://staging.syncfusion.com/support/forums/grid-windows/28278/sorting-in-griddataboundgrid
or
http://staging.syncfusion.com/support/forums/grid-windows/28278/sorting-in-griddataboundgrid---again.


I cannot switch on GGC, because I use a GDBG descendant grid in my project and there are too much logic to move.

Thank you,
Vit


3 Replies

VI Vitaly March 22, 2012 03:56 PM UTC

Not necessary and example, any guidelines will be appreciated.




VI Vitaly March 23, 2012 08:03 AM UTC

Well, I've solved the problem by handling CellDoubleClick event where the e.RowIndex is equal to zero.

There I sort my collection and reassign the datasource.

private void selGridDataBoundGrid_CellDoubleClick(object sender, GridCellClickEventArgs e)
{
if (e.RowIndex == 0)
{
var sortCol = selGridDataBoundGrid.GridBoundColumns[e.ColIndex - 1].MappingName;
queue.SortColumn = sortCol;
queue.SortOrder = (queue.SortOrder == SortOrder.Ascending) ? SortOrder.Descending : SortOrder.Ascending;
selGridDataBoundGrid.DataSource = queue.GetSortedRequests();
e.Cancel = true;
}
}

I found the code for dynamyc Linq sorting on stackoverflow:

public static IOrderedQueryable OrderBy(this IQueryable source, string property)
{
return ApplyOrder(source, property, "OrderBy");
}
public static IOrderedQueryable OrderByDescending(this IQueryable source, string property)
{
return ApplyOrder(source, property, "OrderByDescending");
}
public static IOrderedQueryable ThenBy(this IOrderedQueryable source, string property)
{
return ApplyOrder(source, property, "ThenBy");
}
public static IOrderedQueryable ThenByDescending(this IOrderedQueryable source, string property)
{
return ApplyOrder(source, property, "ThenByDescending");
}
static IOrderedQueryable ApplyOrder(IQueryable source, string property, string methodName) {
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach(string prop in props) {
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] {source, lambda});
return (IOrderedQueryable)result;

By Marc Gravell
http://stackoverflow.com/questions/41244/dynamic-linq-orderby

http://stackoverflow.com/questions/41244/dynamic-linq-orderby






MC Mercy C Syncfusion Team March 27, 2012 01:08 PM UTC

Hi Vit,

Thanks for your interest in Syncfusion products.

We are glad to hear that you have resolved the custom collection sorting in DataBoundGrid with "CellDoubleClick" event.

Also, Please refer to the sample in the following link which makes use of "CellClick" and "PrepareViewStyleInfo" events to accomplish custom sorting.

testgdbg66269254.ziphttp://www.syncfusion.com/downloads/Support/DirectTrac/92519/testgdbg62134213.zip

Please let me know if you have any concerns.

Regards,
Mercy.C


Loader.
Up arrow icon