ApplySortCore for BindingList
I have used the SortableBindingList in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms02182005.asp
to implment the sorting and would like to know how to find out the sorting status ascending or descending from this class. Because it seems that whenever I click on a header it calls the method ApplySortCore with the parameters PropertyDescriptor property and ListSortDirection direction with the direction always set as Ascending. Thus how do I check the status of previous sorting so that I can do an alternate sort direction each time a header is clicked. There is a ListSortDirection SortDirection { get; } to find out the status but not sure how to use it.Thanks
Regards
Alu
When determining what ListSortDirection to pass to ApplySortCore, the BindingList compares the PropertyDescriptor that is the current sort to the new PropertyDescriptor being sorted on. Are you using custom property descriptors anywhere in your implementation?
I dont think i am using any custom property descriptors. Codes are followed closely with the sample given. Currently I c reate a boolean variable to capture the state of the sorting order if not , the sorting orders is always ascending ie not working. :(
Regards
Alu
You need to override the SortDirectionCore property as well as the SortPropertyCore property. Also, you will need to assign values in ApplySortCore() and RemoveSortCore(). See code below...
private ListSortDirection _sortDirection;
private PropertyDescriptor _sortProperty;protected override ListSortDirection SortDirectionCore
{
get { return _sortDirection; }
}protected override PropertyDescriptor SortPropertyCore
{
get { return _sortProperty; }
}
protected
override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
_sortProperty = property;
_sortDirection = direction;
// Get list to sort
List<T> items = this.Items as List<T>; // Apply and set the sort, if items to sort
if (items != null)
{
PropertyComparer<T> pc = new PropertyComparer<T>(property, direction);
items.Sort(pc);
_isSorted = true;
}
else
{
_isSorted = false;
}
// Let bound controls know they should refresh their views
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}protected
override void RemoveSortCore()
{
_isSorted = false;
_sortProperty = null;
}