473,409 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Sorting DataGrid bound to collection

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
Nov 16 '05 #1
7 4201
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:eP********************@giganews.com...
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

Nov 16 '05 #2
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:uo****************@TK2MSFTNGP14.phx.gbl...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:eP********************@giganews.com...
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


Nov 16 '05 #3
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:md********************@giganews.com...
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:uo****************@TK2MSFTNGP14.phx.gbl...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:eP********************@giganews.com...
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



Nov 16 '05 #4
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:bb********************@giganews.com...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:md********************@giganews.com...
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:uo****************@TK2MSFTNGP14.phx.gbl...
> 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" <pd******@NOSPAM.hotmail.com> wrote in message
> news:eP********************@giganews.com...
> >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
> >
> >
>
>



Nov 16 '05 #5
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:en****************@TK2MSFTNGP09.phx.gbl...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:bb********************@giganews.com...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:md********************@giganews.com...
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:uo****************@TK2MSFTNGP14.phx.gbl...
> 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" <pd******@NOSPAM.hotmail.com> wrote in message
> news:eP********************@giganews.com...
> >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
> >
> >
>
>



Nov 16 '05 #6
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:A9********************@giganews.com...
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:en****************@TK2MSFTNGP09.phx.gbl...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:bb********************@giganews.com...
> 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" <pd******@NOSPAM.hotmail.com> wrote in message
> news:md********************@giganews.com...
>> 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:uo****************@TK2MSFTNGP14.phx.gbl...
>> > 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" <pd******@NOSPAM.hotmail.com> wrote in message
>> > news:eP********************@giganews.com...
>> > >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
>> > >
>> > >
>> >
>> >
>>
>>
>
>



Nov 16 '05 #7
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 )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2******************@tk2msftngp13.phx.gbl...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:A9********************@giganews.com...
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:en****************@TK2MSFTNGP09.phx.gbl...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:bb********************@giganews.com...
> 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" <pd******@NOSPAM.hotmail.com> wrote in message
> news:md********************@giganews.com...
>> 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:uo****************@TK2MSFTNGP14.phx.gbl...
>> > 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" <pd******@NOSPAM.hotmail.com> wrote in message
>> > news:eP********************@giganews.com...
>> > >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
>> > >
>> > >
>> >
>> >
>>
>>
>
>



Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phillip | last post by:
I have a DataGrid bound to a custom collection that implements IBindingList. The collection returns true for AllowNew. The collections AddNew() method creates a new object, adds it to the...
10
by: ShadowsOfTheBeast | last post by:
hi all i am trying to bind data to my datagrid from a listbox which i think it should work but an error is coming up saying i have to bind to a datasource that implements the Inumerable or...
0
by: Oliver Hopton | last post by:
Hello, I'm trying to write a PageableCollection class which implements paging for me so I don't need to rely on the paging provided by the DataGrid as that generates huge viewstate when bound to...
2
by: awiklund | last post by:
Hi, I am sorting an arrayList using a custom comparer, but it sort the element in a uncommon fashion. Take a look at this code. using System; namespace ConsoleApplication8
4
by: Mark Travis | last post by:
Hi all, I have written a simple Web Application that displays a query result onto a page using the ASP DataGrid. To Digress ======= Development information about the page is as follows 1....
2
by: pmanno | last post by:
If I have a page with a DataGrid that is bound to a DataTable that is populated by a query to a database and I want to enable sorting and paging, do I have to add the DataTable to the cache or will...
11
by: Fred Nelson | last post by:
I have an application in which it would be VERY beneficial if I could obtain the names of the colums in a datagrid. For example dim datagrid1 as new datagrid datagrid1.datasource = (stored...
0
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file,...
4
by: gane | last post by:
Hi, I am creating datagrid bound column dynamically and need to check if a datagrid column already exists?Is there a way to check this? thanks gane
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.