By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,645 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,645 IT Pros & Developers. It's quick & easy.

strongly typed collection -- sort in a DataGrid

P: n/a
Assume you have a strongly typed collection of a class called Person. The
strongly typed collection implements IEnumerable so it can be databound to a
server control like a DataGrid. The Person class has several public
properties like FirstName, LastName, and Gender.

What steps would it take to allow the collection to be sorted in multiple
ways when bound to a DataGrid? In the past, I used the sort property of a
DataView containing a datatable.

Thanks in advance.

Mark
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
This should do the trick ....

http://dotnetjunkies.com/WebLog/bsbl.../02/24116.aspx
"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Assume you have a strongly typed collection of a class called Person. The
strongly typed collection implements IEnumerable so it can be databound to a server control like a DataGrid. The Person class has several public
properties like FirstName, LastName, and Gender.

What steps would it take to allow the collection to be sorted in multiple
ways when bound to a DataGrid? In the past, I used the sort property of a
DataView containing a datatable.

Thanks in advance.

Mark

Nov 16 '05 #2

P: n/a
Hi Mark,

I have another solution, with this you can sort any collection without need
to change anything in the class being sorted. I only use reflection.

A very good thing about it is that you can sort using a property of a
property , if your Person class has a property of type Address you could
sort it by Person.Property.Zip for example.

Here I send you the code. and a brief way of using it.

Hope this help,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


This is how easy it's to use: ( it's a method of the strong t yped
collection )

public CostCenterCollection Sort( string sortParam, TSRCore.SortDirection
direction)
{
ArrayList newlist = (ArrayList)InnerList.Clone();
ClassSorter sorter = new ClassSorter( sortParam, SortByType.Property,
direction);
newlist.Sort( sorter);
return new CostCenterCollection( newlist); //This is the strong typed
collection class , I return a new sorted instance
}

As you can see it's extremely easy to use.

ClassSorter
------------------------
This class implement the IComparer interface, it use reflection to get the
value, it recieve the name of the property to evaluate in a parameter in the
constructor:

public class ClassSorter: IComparer
{
protected string sortBy;
protected SortByType sortByType;
protected SortDirection sortDirection;
#region Constructors
public ClassSorter(string sortBy, SortByType sortByType, SortDirection
sortDirection)
{
this.sortBy = sortBy;
this.sortByType = sortByType;
this.sortDirection = sortDirection;
}
#endregion

int Compare( object x, object y, string comparer)
{
if ( (x== null ) && (y==null) )
return 0;
if ( x == null )
return -1;
if ( y == null )
return 1;
if ( comparer.IndexOf( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().GetProperty( parts[0]).GetValue(x, null) ,
y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
IComparable icx, icy;
icx =
(IComparable)x.GetType().GetProperty( comparer).GetValue(x, null);
icy =
(IComparable)y.GetType().GetProperty( comparer).GetValue(y, null);

if ( x.GetType().GetProperty(comparer).PropertyType ==
typeof(System.String) )
{
icx = (IComparable) icx.ToString().ToUpper();
icy = (IComparable) icy.ToString().ToUpper();
}

if(this.sortDirection == SortDirection.Descending)
return icy.CompareTo(icx);
else
return icx.CompareTo(icy);
}

}

public int Compare(object x, object y)
{
return Compare( x, y, sortBy);
}

}

public enum SortByType
{
Method = 0,
Property = 1
}

public enum SortDirection
{
Ascending = 0,
Descending = 1
}
"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Assume you have a strongly typed collection of a class called Person. The
strongly typed collection implements IEnumerable so it can be databound to a server control like a DataGrid. The Person class has several public
properties like FirstName, LastName, and Gender.

What steps would it take to allow the collection to be sorted in multiple
ways when bound to a DataGrid? In the past, I used the sort property of a
DataView containing a datatable.

Thanks in advance.

Mark

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.