strongly typed collection -- sort in a DataGrid

M

Mark

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top