 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 9th, 2008, 08:10 AM
|
|
Registered User
|
|
Join Date: May 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorting GridView with a Collection as DataSource+
Hello, I'm trying to sort my GridView and I have an ObjectDataSource that receives a List<> in the return of the Select Method. Everything is workin' fine, but I recieve an Exception when I try to sort my GridView and searching through the web, I realize that the GridView does not perform the automatic sorting when working with List<> objects. What I would like to know is if there is anyone that have this problem and, if so, have found a solution.
Thanks
|
|

June 9th, 2008, 02:46 PM
|
|
Registered User
|
|
Join Date: May 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Very nice. The article helped me to came with a solution for my problem. I just made some changes and build a unique class that I can use to sort any custom object, but the philosophy is the same. Thanks a lot!
|
|

June 10th, 2008, 07:06 AM
|
|
Registered User
|
|
Join Date: May 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sure. I was working with a class that I grab from another article, but this class wasn't working for my needs, because it doesn't work with ObjectDataSource. And with your article, the solution works fine for ObjectDataSources, but in my project I will have to do the custom sorting for several classes and what I wanted was something reusable. Then, I just adapted the two approaches into a single class. That's what I did:
First I've created a class that implements the IComparer interface:
Code:
1 using System;
2 using System.Reflection;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Web.UI.WebControls;
6
7 public class GenericComparer<T> : IComparer<T>
8 {
9 string sortExpression;
10 SortDirection sortDirection;
11
12 public GenericComparer(string sortExpression)
13 {
14 if (sortExpression.ToLowerInvariant().EndsWith(" desc"))
15 {
16 this.sortExpression = sortExpression.Substring(0, sortExpression.Length - 5);
17 sortDirection = SortDirection.Descending;
18 }
19 else
20 {
21 this.sortExpression = sortExpression;
22 sortDirection = SortDirection.Ascending;
23 }
24 }
25
26 public int Compare(T x, T y)
27 {
28 PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression);
29 IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
30 IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);
31 if (sortDirection == SortDirection.Ascending)
32 {
33 return obj1.CompareTo(obj2);
34 }
35 else
36 {
37 return obj2.CompareTo(obj1);
38 }
39 }
40 }
Than in my business layer, my select method stayed like this:
Code:
public static ClientList getList(string sortExpression)
{
ClientList clients = ClientDB.getList();
if (sortExpression.Length > 0)
{
clients.Sort(new GenericComparer(sortExpression));
}
return clients;
}
|
|

June 10th, 2008, 01:11 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Are you sure that code compiles? Don't you need a <T> type parameter for the Compare method to work:
Code:
public class GenericComparer<T> : IComparer<T>
{
...
}
Now that it's generic, it may be worth adding an overload for Sort on the List<T> class so you can simple do stuff like this:
Code:
List<Person> myList = new List<Person>();
myList.Add(new Person(){FirstName="Imar", LastName="Spaanjaars"});
myList.Add(new Person(){FirstName="Daniel", LastName="Pilon"});
myList.Sort("FirstName");
All you need is an extension method like this:
Code:
public static class ListExtensions
{
public static void Sort<T>(this List<T> theList, string sortExpression)
{
if (!string.IsNullOrEmpty(sortExpression))
{
theList.Sort(new GenericComparer<T>(sortExpression));
}
}
}
and a using statement like this in the class that needs the extension overload:
Sweet...... ;)
If I find some time this weekend, I'll write a slightly longer blog about this.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|
|

June 10th, 2008, 02:03 PM
|
|
Registered User
|
|
Join Date: May 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The problem is that my code is written in portuguese and to post it here I translate some expressions and I cut off the <T> type parameter, but, yes, I have it on my class. Sorry, my mistake. And I really enjoy the extensions idea. Thanks again!
|
|
 |