473,386 Members | 1,736 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,386 software developers and data experts.

Sort a BindingList

Hello:

I want to know the best way to sort a BindingList without needed to
turn it into a DataTable. Of course, my BindingList is being used
indirectly by a BindingSource.

So, how do I sort a BindingList of my custom business objects? For
instance, when the user clicks on a column header of a DataGridView, I
would like sorting to be handled for me (or to call a custom sort
method I provide).

If this isn't supported, that would be good to know.

Thanks,
Travis
Jan 2 '08 #1
4 26364
If I remember, it isn't supported out-of-the-box, but you can inherit
from BindingList and provide an implementation for sort (and the
matching "is supported" property to return true). The trick is to
provide suitable implementations for IBindingList (single sort) and/or
IBindingListView (multi-sort).

I have a full implementation, but can't access it until tomorrow. Let
me know if you want me to post some of this (although it isn't too
hard).

Marc
Jan 2 '08 #2
You're right. It is fairly simple. Thanks for the information.

public class SortableBindingList<T: BindingList<T>
{
protected override bool SupportsSortingCore
{
get
{
return true;
}
}

protected override bool IsSortedCore
{
get
{
for (int i = 0; i < Items.Count - 1; ++i)
{
T lhs = Items[i];
T rhs = Items[i + 1];
PropertyDescriptor property = SortPropertyCore;
if (property != null)
{
object lhsValue = lhs == null ? null :
property.GetValue(lhs);
object rhsValue = rhs == null ? null :
property.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result =
Comparer.Default.Compare(lhsValue, rhsValue);
}
if (SortDirectionCore ==
ListSortDirection.Descending)
{
result = -result;
}
if (result >= 0)
{
return false;
}
}
}
return true;
}
}

private ListSortDirection sortDirection;
protected override ListSortDirection SortDirectionCore
{
get
{
return sortDirection;
}
}

private PropertyDescriptor sortProperty;
protected override PropertyDescriptor SortPropertyCore
{
get
{
return sortProperty;
}
}

protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
{
sortProperty = prop;
sortDirection = direction;

List<Tlist = (List<T>)Items;
list.Sort(delegate(T lhs, T rhs)
{
if (sortProperty != null)
{
object lhsValue = lhs == null ? null :
sortProperty.GetValue(lhs);
object rhsValue = rhs == null ? null :
sortProperty.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result = Comparer.Default.Compare(lhsValue,
rhsValue);
}
if (sortDirection == ListSortDirection.Descending)
{
result = -result;
}
return result;
}
else
{
return 0;
}
});
}

protected override void RemoveSortCore()
{
sortDirection = ListSortDirection.Ascending;
sortProperty = null;
}
}
Jan 2 '08 #3
On Jan 2, 11:11*am, Marc Gravell <marc.grav...@gmail.comwrote:
If I remember, it isn't supported out-of-the-box, but you can inherit
from BindingList and provide an implementation for sort (and the
matching "is supported" property to return true). The trick is to
provide suitable implementations for IBindingList (single sort) and/or
IBindingListView (multi-sort).

I have a full implementation, but can't access it until tomorrow. Let
me know if you want me to post some of this (although it isn't too
hard).

Marc
could you please post the multi sort example...thanks in advance!!
Jan 3 '08 #4
I will dig some code out - but basically it involves writing a
composite (chaining) IComparer<T- i.e. for each property in turn it
does the comparison (as you have already), until a non-zero is found
(or the last comparer (= last property) is found).

Annoyingly, I have a *near* identical block of code "on hand" for some
3.5 LINQ stuff, but alas the gap between "near" and "is" (plus the 3.5
link) is too far to be tweaked quickly...

I'm just trying to seee what I can find... shouldn't bee too long...

Marc
Jan 4 '08 #5

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

Similar topics

1
by: Andreas Zita | last post by:
Hi I have a simple problem scenario with yet no good solution: I want: A sorted (by name) and bindable typesafe collection (of students) which I can set as DataSource to a ListBox. When...
4
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view...
1
by: Dave Booker | last post by:
Is there a reason why the BindingList constructor doesn't create a new BindingList? Create a form with four ListBoxes and the following will result in them all containing the exact same thing. ...
1
by: Pieter | last post by:
Hi, I have a custom List that inherits from BindingList. It has some methods overloaded, like the Add/Insert/etc to add and remove some eventhandlers when adding or removing an item T of the...
3
by: Bryan | last post by:
I am calling BindingList.EndEdit during a 'TextChanged' sub for a texbox. In theory, the key should be pressed changing the value in the text box, the EndEdit call should append the changes to the...
5
by: Mike Surcouf | last post by:
Hi All I have a stored procedure wrapper that returns Collection<T>. The wrapper is generated by codesmith so I don't really want to start altering it. I need to use this collection in a...
6
by: jwilson128 | last post by:
I am trying to decide whether to use a system.ComponentModel.BindingList(of T) or a Sytem.Collections.Generic.List(of T) for a custom collection. In testing a simple, read-only data binding to a...
9
by: active | last post by:
Can you see why this does not sort the list? It displays OK but is not sorted Thanks for any help
1
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I have a ComboBox named comboBoxSelChannel. I declared a structure named MySturct. public struct MyStruct { public int Index; public string Name;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.