Sorting DataGrid bound to collection

P

Pete Davis

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
 
I

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

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
}
 
P

Pete Davis

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 ) said:
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
}





Pete Davis said:
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
 
P

Pete Davis

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

Pete Davis said:
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 news:uoPC6dk%[email protected]...
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
}





Pete Davis said:
A different question this time. I have a DataGrid bound to a
collection.
 
I

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

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



Pete Davis said:
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

Pete Davis said:
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 news:uoPC6dk%[email protected]...
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
 
P

Pete Davis

There is no Bind() method in DataGrid, at least not that I'm seeing.

Setting the DataSource binds the collection to the grid, but updates aren't
automatically detected by the grid because the CollectionBase (and my
collection for that matter) doesn't implement IBindingList.

Pete

Ignacio Machin ( .NET/ C# MVP ) said:
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



Pete Davis said:
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

Pete Davis said:
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
 
I

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

Hi,

Sorry pete, it;s DataBind()

Detecting updates could be done doing something like this:
1- Declare a public event in the collection , when a new elem is added you
can fire this event, the client( the form where the grid is hosted )
subscribe to it and react accordingly.

Detecting a modification is more difficult if not impossible to do though.

Cheers,

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


Pete Davis said:
There is no Bind() method in DataGrid, at least not that I'm seeing.

Setting the DataSource binds the collection to the grid, but updates
aren't
automatically detected by the grid because the CollectionBase (and my
collection for that matter) doesn't implement IBindingList.

Pete

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:enQ5QEl%[email protected]...
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



Pete Davis said:
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
 
P

Pete Davis

Sorry, I guess there was some confusion. I'm using Windows forms, not Web
forms. WinForms controls don't have DataBind().

Thanks anyway. I ended up just implementing IBindingList in the collection
and that took care of handling both the sorting and the updating issues.

Pete

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Sorry pete, it;s DataBind()

Detecting updates could be done doing something like this:
1- Declare a public event in the collection , when a new elem is added you
can fire this event, the client( the form where the grid is hosted )
subscribe to it and react accordingly.

Detecting a modification is more difficult if not impossible to do though.

Cheers,

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


Pete Davis said:
There is no Bind() method in DataGrid, at least not that I'm seeing.

Setting the DataSource binds the collection to the grid, but updates
aren't
automatically detected by the grid because the CollectionBase (and my
collection for that matter) doesn't implement IBindingList.

Pete

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:enQ5QEl%[email protected]...
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
 

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