tags:

views:

367

answers:

4

Hello, I got a class let's call it Blah. I use ObjectDataSource to get the data from the database and the value is bind it to Blah's property for example, Column Foo is Blah.Foo in my class. I can bind ObjectDataSource with a list of Blah to a gridview but i can't make it sortable since the datasource is not datatable. Does anyone have link or tutorial on how to make object to be sortable for control like gridview.

thank

Jack

A: 

You need to implement the IComparable interfaces. There may be other interfaces you need to implement, but they will all be in the same vein.

hova
+1  A: 

First you could use the AllowSorting property of the gridview with SortExpression on your fields. Here is a tutorial

Second, you could made a generic list of Blah and then call .Sort()

List<Blah> test = new List<Blah>;
test.AddRange(ObjectDataSource);
test.Sort();

msdn information

Third you could sort the gridview directly from an expression like

CustomersGridView.Sort(expression, direction);

link

lucian.jp
A: 

You have to implement Icomparable then pass the sort expression from the gridview to your custom sort method.

Imar Spaanjaars has a nice tutorial and code examples of creating sortable business object collections that you can bind to asp.net controls

Element
A: 

Hi,

You may want to try to sort your list using anonymous delegates:

List<YourObject> MyList = new List<YourObject>();

MyList.Sort(delegate(YourObject o1, YourObject o2)
{ return o1.SomeProperty.CompareTo(o2.SomeProperty);});

More about anonymous and delegate: MSDN

afgallo