Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
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
 
Old September 18th, 2007, 12:51 PM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old September 18th, 2007, 01:28 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

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

For a detailed explanation of what it is sorting, you may want to check out the series this article belongs to: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416

Cheers,

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
 
Old September 18th, 2007, 04:50 PM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old September 18th, 2007, 05:01 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
 
Old September 18th, 2007, 05:09 PM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old September 18th, 2007, 05:14 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
 
Old September 18th, 2007, 05:35 PM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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..!
 
Old September 18th, 2007, 07:35 PM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old September 19th, 2007, 01:34 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Great; glad you got it working.

Have fun,

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
 
Old September 20th, 2007, 12:32 PM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?





Similar Threads
Thread Thread Starter Forum Replies Last Post
strongly-typed DataSet.Rows r n't getting Updated srkvellanki ASP.NET 2.0 Professional 3 October 1st, 2008 05:26 PM
"Strongly typed" roman BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 June 26th, 2008 10:35 AM
Record Locking & Transactions in Strongly Typed DS Kia Visual Basic 2005 Basics 4 July 23rd, 2007 06:23 AM
Accessing Strongly Typed Session Data Woodman ASP.NET 2.0 Professional 2 March 9th, 2007 04:42 PM
Crystal Report and Strongly-Typed dataset jaucourt Crystal Reports 1 February 9th, 2004 07:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.