how do you sort a generic list? binding to gridview/listview?

  • Thread starter Thread starter ethem
  • Start date Start date
E

ethem

Hi there,

how to sort a generic list?

I'm using generics (class definition) and binding to this generics
class.
so the factory returns a dictionary <key, genericclass>

e.g.

key genericclass
1000 1000-companynameA-cityA-countryA-PhoneA-FaxA (6 property
definition in the class)
1001 1001-companynameB-cityB-countryB-PhoneB-FaxB
etc.

I'm binding to asp:gridview and asp:listview. it works....

but I won't to sort when the header is clicked. How can I achieve
this? e.g. Country is clicked... I get in argument the value
country.... but how can I sort the generic list ASC/DESC to country?

I did search on 'sort on generics list ' but not find really the
answer...

any help or direction is appreciated....

thanks
 
in addtion here is the place I need to handle the sorting:


protected void gvAddress_Sorting(object sender,
GridViewSortEventArgs e)
{

}

In Debug I see these values:
========================
(sender as Gridview).Datasource = null

e.SortDirection = Ascending
e.SortExpression = AddressID
 
Hi,
You would need to implement the Icomparer interface. By default
when you call the sort method of a list , it would be sorted according
to the comparer attached. If no comparer is attached, then
it would do default sorting.
To implement what you require you would have to pass the column name
to the comparer, and instruct it to sort on the field from which the
column is populated.
Also you would also need to take care of the sort direction i.e
whether the sorting is ascendnig or descending.
 
Back
Top