hi
you do not need to set it to null, just set the datasource to the collection
and call bind
yes, that is the only method
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Ignacio,
That worked beautifully. Handled the click, created the ClassComparer
based on the column header that was clicked, sorted, and re-bound the
grid.
One question (and I posted this same thing in regards to a listbox
earlier). Is there any way to get the grid to update when there are
changes
made to the collection? The only method I've found is to set the
DataSource
to null and then set it back to the collection.
Thanks.
Pete
Very nice class. Thanks.
So, what you're saying is that I need to handle the click on the
column
headers and handle the sorting myself, though, right?
Thanks.
Pete
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message Hi,
Simply, sort the collection
Use the class below to sort a collection , it use reflection.
I have a better documented version but it's at home, if you have any
doubt
of how/why it please post back with your comments.
It's very simple you create a new ClassComparer and say what
property
of
the
class you want to use for it, you can even use compound expressions
like
"BirthDate.Year" it goes down recursively.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
You may use it like this ( this method belong to a strong typed
collection
of a type named LocationPoint )
public LocationPointCollection Sort( string sortParam, SortDirection
direction)
{
ArrayList newlist = (ArrayList)InnerList.Clone();
ClassSorter sorter = new ClassSorter( sortParam,
SortByType.Property,
direction);
newlist.Sort( sorter);
return new LocationPointCollection( newlist);
}
This is the class
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
}
A different question this time. I have a DataGrid bound to a
collection.
Is
there any way for me to allow sorting? The DataGrid.AllowSorting=true
doesn't work, but that's probably because it can't assume the data
types
and
thus can't sort them.
I thought about implementing IComparable, but I don't see how that
would
work since it doesn't apply generically to all the fields/properties
of
the
class.
Thanks.
Pete