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 4 26342
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
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;
}
}
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!!
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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
|
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;
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |