 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional 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
|
|
|
|

September 18th, 2007, 12:51 PM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorting Gridview bound to strongly typed object
Hi all,
I have a gridview, whose datasource is a strongly typed object (container) which is a generic list of entity objects.
Now i need to achieve sorting on this gridview cloumns...
would anyone send me the code to implement Icomparable interface..
Thanks,
--Monika
|
|

September 18th, 2007, 04:50 PM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
The article looks very good.
I implemented it exactly in my code but I am getting a complier error for the statement.
mylist.Sort(new MyComparer(sortExpression));
Error 1 The best overloaded method match for 'System.Collections.Generic.List<EFS.Core.CoreEnti ty>.Sort(System.Collections.Generic.IComparer<EFS. Core.CoreEntity>)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'MyComparer' to 'System.Collections.Generic.IComparer<EFS.Core.Cor eEntity>'
Where CoreEntity is the parent class of MyEntity.
The comparer class looks like this.
public class MyComparer:CoreEntity,IComparer<MyEntity>
{
public MyComparer(string sortExpression)
{
//some code
}
public int Compare(MyEntity a, MyEntity b)
{
//some code
}
}
So the statement :
mylist.Sort(new MyComparer(sortExpression));
throws complier error.
(mylist is a generic List of MyEntity objects)
pls let me know if I am doing something wrong or missing anything.
Thanks,
--Monika
|
|

September 18th, 2007, 05:01 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Did you notice that the ContactPersonComparer class has been implemented as a private nested class within the ContactPersonManager class?
Also, in my case, ContactPersonManager is a Manager class that manages ContactPerson objects not included in the code download for the Sorting article.
Maybe that gives you a clue?
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

September 18th, 2007, 05:09 PM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes..i read your code closely ...it is very similair to my requirement and I m doing exactly what you have done.!
Then what is that I am missing .? I still cant get to fix that error.
Thanks,
--Monika.
|
|

September 18th, 2007, 05:14 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Well, not entirely. Your comparer class inherits from your own entity base class:
public class MyComparer:CoreEntity,IComparer<MyEntity>
Mine doesn't; it simply implements IComparer<MyEntity>
Not sure what's going on though. Hard to tell with code on simple grey text area without color coding, IntelliSense, a debugger and fresh coffee to keep me awake.....
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

September 18th, 2007, 05:35 PM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok...Would your code be still the same and work if your ContactPerson class inherits a parent class say ContactPerson :parentPerson
and also the ContactPersonManger class inherits its own parent class
ContactPersonManger:parentManager
Thats the only difference between ur code and my code..
I have changed my comparer to be like yours.
private class MyComparer:IComparer<MyEntity>
So no idea whats going wrong why that error is coming..!
|
|

September 18th, 2007, 07:35 PM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hey I got it working..
Thank you for your code...
The fix to my error was:
Instead of having
public class MyComparer:IComparer<MyEntity>
I had to do:
public class MyComparer:IComparer<MyParentEntity>
Since MyEntity is child of MyParentEntity.
and in the Compare method I just had to typecast it to Myentity type.
So instead of :
Compare(MyEntity x, MyEntity y) it had to be
Compare(MyParentEntity x, MyParentEntity y)
{
MyEntity entityX = MyEntity(x);
MyEntity entityY = MyEntity(y);
}
And it works great now...
Thanks Imar...u have alwayz been a great help!
Regards,
--Monika
|
|

September 20th, 2007, 12:32 PM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I also wanted to have a up/down arrow on my sortable columns to show the users direction of sorting..!
is it possible to have something like that?
Also, while Paging/sorting...i need to do a gridview.datasource ans gridview.databind in the event handlers..!
So , i either need to load the whole data again or need to have a way to storing it..
Right now I m storing it in Session during first load and using it the the paging/sorting event handlers..
any better way of doing this?
|
|
 |