views:

1192

answers:

5

I have a problem with Gridview sorting that is similar to others but I'm binding to a collection object as opposed to a data table.

The existing business rules and data access layers of an application follow the pattern of having an object and, if you need a collection of objects of that type, to have another class inheriting CollectionBase and implementing IBindingList.

For desktop applications, it was easy to databind a gridview to one of these objects and there weren't any problems with turning on column sorting. Everything was 'in state' in the desktop app's presentation layer.

Now that code is being moved to a new web application (ASP.NET 2.0, VB codebehind pages).

I've played around with what I had to do to only have certain columns of the collection show up in the gridview and the gridview looked pretty good. When I turned on 'allow sorting', that's when the problems showed up.

I'm getting the error about not having a .Sorting method, etc. In researching this, I found all sorts of solutions that were easily implemented with dataviews if my source was a data table. But it's not - it's a collection. I tried to "cheap shot" a datasource by converting the collection to an XML memory stream and them trying to .ReadXML back into a dataset but that didn't work [Root element is missing error was as far as I got in the dataset.ReadXml(ioTemp) where ioTemp was the System.IO.MemoryStream used in the xml serializer].

Because of the old desktop apps, I've never had to worry about sorting a collection since the gridview handled it once it was loaded. In fact, it's a 'standard' that the collection's .SortProperty, .SortDirection and .ApplySort all through NotSupportedExceptions (I inherited this code from programmers long gone).

Is there an easy way to convert the collection to a data table or a way to sort the collection without having to go back to the database each time? Object Data Sources won't work becuase of the intricate rules in how the objects are built - the wizards in VS2005 just can't handle what we need to do (grabbing data from several tables conditionally to make an object).

Thanks in advance.

A: 

I had a similar issue and i needed to implement IComparable on the objects. Basically to sort a collection of objects you need a way to distinguish their order. The IComparable interface has one method called Compare which allows the .Net framework to work out the order of the objects when you sort them. You need to implement this method yourself to get the sort method to work.

Google results

You don't mention the error message so i cant be sure if this is the case, can you post the error?

EDIT :

In regards to your comment; you can implement multi column sorting, it just requires more work. You can specify the fields to sort the collection by and then use this information within the CompareTo Method.

Have a look at this

Kaius
ICompare would be good for a simple sort but this case is for sorting on any column they click on in the grid. There HAS to be a simple way to sort a grid that is already on the screen...
David
You'd think so, but I think Kaius is correct unfortunately. I'll try to take a peek at one of my projects tomorrow.
tbone
A: 

Given that you apparently are populating the grid with a collection of your own objects, this sounds like a perfect job for Linq for Objects. With just a little elbow grease you can achieve what is effectively an SQL Select statement against your collection. Very cool stuff.

http://www.hookedonlinq.com/LINQtoObjects5MinuteOverview.ashx

Also, do you really just want to sort the data in the grid? If so, then'd definitely pursue using Linq against your objects. However, rarely does sorting the contents of the grid really answer the problem ("sorting the grid" usually translates into changing the access path of the data used to fill the grid.) Browser apps aren't like Windows apps and don't have a full-time connection to the underlying data source to make things happen quite as magically as the DataGridView in Windows makes things seem.

rp
Perhaps I could use this challenge to convince the Powers That Be that we should be going 'more modern' than ASP.NET 2.0. Unfortunately, this is a government installation.
David
+1  A: 

Have you considered client side sorting instead?

I have used the jquery tablesorter plugin in the past with ASP Gridviews.

http://tablesorter.com/

Sean Taylor
A: 

You can put link buttons with an On_Click event as the header's of each column.

When the event is triggered, figure out which header was clicked on (one method per header or a commandArgument value). Once that is know, do a .orderBy or .OrderByDescending by on the collection of objects, and put the result back in as datasource of the gridview and databind on that.

Stephen B. Burris Jr.
A: 

In the year since I originally asked this question, I managed to get a new 'standard' implemented so that collections of business objects were now generic lists.

So now a "Collection class" that is little more than a "Inherits List(Of MyBusinessObject)" with a Sort Method that looks like this (performance wasn't an issue):

Public Overloads Sub Sort(ByVal strPropertyName As String, ByVal strDirection As String)
    Dim arSortedList As New ArrayList
    For Each item As MyBusinessObject In Me
        arSortedList.Add(item)
    Next
    arSortedList.Sort(New CaseInsensitiveComparer(Of MyBusinessObject)(strPropertyName, strDirection))
    For intI As Integer = 0 To arSortedList.Count - 1
        Item(intI) = arSortedList(intI)
    Next
End Sub

This seemed to work perfectly with the methodology used by the GridView for firing events.

David