Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 9th, 2008, 08:10 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

Reply With Quote
  #2 (permalink)  
Old June 9th, 2008, 12:37 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Take a look here: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=428

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.
Reply With Quote
  #3 (permalink)  
Old June 9th, 2008, 02:46 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!

Reply With Quote
  #4 (permalink)  
Old June 9th, 2008, 03:51 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Glad you got it working.

Would you mind showing your final code with this forum?

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.
Reply With Quote
  #5 (permalink)  
Old June 10th, 2008, 07:06 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;
        }
Reply With Quote
  #6 (permalink)  
Old June 10th, 2008, 01:11 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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:
Code:
using MyExtensons;
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.
Reply With Quote
  #7 (permalink)  
Old June 10th, 2008, 02:03 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!

Reply With Quote
  #8 (permalink)  
Old June 10th, 2008, 04:20 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome, and thank you too.

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disp Footer even no record in GridView Datasource mallikalapati ASP.NET 2.0 Professional 1 March 19th, 2008 02:23 AM
chapter about object datasource and gridview? asptwodev BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 November 2nd, 2007 12:53 PM
Gridview for inser,update without using datasource karthikeyan_pa ASP.NET 2.0 Professional 0 September 4th, 2007 07:58 AM
Gridview Sorting Event without a Datasource rit01 ASP.NET 2.0 Professional 15 March 22nd, 2007 03:34 AM
GridView DataSource Abbas C# 2005 0 November 28th, 2006 02:54 PM



All times are GMT -4. The time now is 07:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.