Simple column sorting example to use with GridView, CslaDataSource and ReadOnlyListBase

Simple column sorting example to use with GridView, CslaDataSource and ReadOnlyListBase

Old forum URL: forums.lhotka.net/forums/t/7964.aspx


j055 posted on Monday, November 09, 2009

Hi

Does anyone have a simple example to enable sorting when using ASP.NET GridView, CslaDataSource and a ReadOnlyListBase derived class without modifing the ReadOnlyListBase class?

Many thanks
Andrew

RockfordLhotka replied on Monday, November 09, 2009

The CSLA .NET Version 2.1 Handbook covers all the sort/filter options pretty thoroughly.

If you can't modify the ROLB, then you eliminate 3 of the 4 options, and must do the sort in the CslaDataSource event handler - get the ROLB and then probably use a LINQ query or SortedBindingList to sort it.

That's the least efficient solution btw, but it is the only one that works without modifying at least the factory and DataPortal_Fetch() method of your collection class.

j055 replied on Tuesday, November 10, 2009

Thanks for that

I put the logic in the CslaDataSource event handler and used the SortedBindingList

protected void RecipientsDataSource_SelectObject(object sender, SelectObjectArgs e)
{
var logDetail = GetLogDetail();

if (!string.IsNullOrEmpty(RecipientsGridView.SortBLOCKED EXPRESSION
{
var sortedList = new SortedBindingList(logDetail.LogRecipients);
var direction = RecipientsGridView.SortDirection == SortDirection.Descending
? ListSortDirection.Descending
: ListSortDirection.Ascending;
sortedList.ApplySort(RecipientsGridView.SortExpression, direction);
e.BusinessObject = sortedList;
}
else
e.BusinessObject = logDetail.LogRecipients;
}

For this particular solution it's fine performance wise.

Cheers
Andrew

Copyright (c) Marimer LLC