GridView Sorting - Help needed with Implementation

GridView Sorting - Help needed with Implementation

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


AndrewR posted on Sunday, November 15, 2009

Hello,

I'm revisiting this issue as I now have to find a solution for my users. I need to allow sorting on the gridview columns. My gridview is bound to a csladatasource. A while back I had found some code that implemented sorting using linq, but it seems the csladatasource does not play nicely with the implementation.

I am hoping someone has a solution for sorting, and hopefully it's implemented on the csladatasource.

I am using csla.net 3.5 with .net 3.5 framework. Please see:

SelfSortingGridView - GridView from www.singingeels.com

Thanks,

Andrew

RockfordLhotka replied on Monday, November 16, 2009

The CSLA .NET Version 2.1 Handbook ebook has a section that walks through the various sort options available when using CSLA. In general the model relies on ASP.NET data binding providing the sort column and direction, along with page and item count values for paging, which it does with the standard Microsoft controls at least, and probably with third party controls I assume.

That information is provided in the arguments to the select event that is raised by the CslaDataSource. In other words, you get that information provided to you in your select event handler. At that point there are four possible places for you to do a sort (or filter):

  1. In the CslaDataProvider select event handler (typically in your code-behind file)
  2. In the business object's static factory method (invoked by your select handler)
  3. In the DataPortal_Fetch() method
  4. In the database

Options 1 and 2 are the least efficient (usually), but are easy to do with LINQ to Objects or the Csla.SortedBindingList class.

Options 3 and 4 are usually much more efficient and how they work depends on your data access technology and/or database.

AndrewR replied on Monday, November 16, 2009

Thanks Rocky. I was able to implement osrting this way:

Protected Sub dsAssetTransferList_SelectObject(ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs) Handles dsAssetTransferList.SelectObject

' get list

Dim list As AssetTransferList = GetAssetTransferList()

' do sort

If String.IsNullOrEmpty(e.SortExpression) Then

' return unsorted list

e.BusinessObject = list

Else

Dim sortedList As New SortedBindingList(Of AssetTransferInfo)(list)

sortedList.ApplySort(e.SortProperty, e.SortDirection)

' return sorted list

e.BusinessObject = sortedList

End If

End Sub

Not the most elegant or exotic, but it works for me especially since the list object is cached; need not do a round trip to SQL.

Thanks,

Andrew.

Copyright (c) Marimer LLC