SortedBindingList, FilteredBindingList inconsistancy

SortedBindingList, FilteredBindingList inconsistancy

Old forum URL: forums.lhotka.net/forums/t/2285.aspx


paulhumphris posted on Tuesday, February 06, 2007

I am trying to use the FilteredBindingList on one of my BusinessObjects that inherits from ReadOnlyListBase. However, once I have created the FilteredBindingList the SupportsSorting property returns false also when the same object is loaded into a SortedBindingList the SupportsSorting property is true.

ProductSearchResultList products = ProductSearchResultList.GetProductSearchResultList("monitor");

FilteredBindingList<ProductSearchResultInfo> filtered = new FilteredBindingList<ProductSearchResultInfo>(products);
bool test = filtered.SupportsSorting;

SortedBindingList<ProductSearchResultInfo> sorted = new SortedBindingList<ProductSearchResultInfo>(products);
bool test2 = sorted.SupportsSorting;

The example code above will return false for "test" and true for "test2".  Since both the FilteredBindingList and the SortedBindingList use an IBindingList to return the SupportsSorting property I am a little confused as to why I am getting different behaviour.

Brian Criswell replied on Tuesday, February 06, 2007

This is because FilteredBindingList provides filtering and SortedBindingList provides sorting.  They are composable however, so you can do this:

ProductSearchResultList products = ProductSearchResultList.GetProductSearchResultList("monitor");

FilteredBindingList<ProductSearchResultInfo> filtered = new FilteredBindingList<ProductSearchResultInfo>(products);
bool test = filtered.SupportsSorting;

SortedBindingList<ProductSearchResultInfo> sorted = new SortedBindingList<ProductSearchResultInfo>(filtered);
bool test2 = sorted.SupportsSorting;

You could also look at the ObjectListView in CSLAcontrib, which does both sorting and filtering of any IList.  This does mean that you lose strong typing as it behaves like a DataView instead of an IList<T>.

paulhumphris replied on Tuesday, February 06, 2007

I see what you are saying and I also found that somewhere else after making the post.

Does raise the question of why the FilteredBindingList.ApplySort method is public if it is not useable?

 

Brian Criswell replied on Tuesday, February 06, 2007

It most likely delegates to the underlying IBindingList if possible.  So if the underlying IBindingList supported sorting, then it would pass the ApplySort method down to it.

ajj3085 replied on Tuesday, February 06, 2007

Actually, I think it throws an InvalidOperationException or something like that if you call ApplySort and AllowSort is false.  If it doesn't, it probably should.

I'm pretty sure I got such an exception when trying to add to a list and AllowNew was false.

Brian Criswell replied on Tuesday, February 06, 2007

I took a look at the source code and it does delegate sorting to the underlying IBindingList if it supports sorting.  So the following code would allow you to call filtered.ApplySort()

ProductSearchResultList products = ProductSearchResultList.GetProductSearchResultList("monitor");

SortedBindingList<ProductSearchResultInfo> sorted = new SortedBindingList<ProductSearchResultInfo>(products);
bool test2 = sorted.SupportsSorting;

FilteredBindingList<ProductSearchResultInfo> filtered = new FilteredBindingList<ProductSearchResultInfo>(sorted);
bool test = filtered.SupportsSorting;


RockfordLhotka replied on Tuesday, February 06, 2007

ajj3085:
Actually, I think it throws an InvalidOperationException or something like that if you call ApplySort and AllowSort is false.  If it doesn't, it probably should.

I'm pretty sure I got such an exception when trying to add to a list and AllowNew was false.

I always delegate the call - relying on the underlying implementation to throw any exceptions - unless the original list isn't an IBindingList, in which case I throw an exception directly.

paulhumphris replied on Wednesday, February 07, 2007

Thanks all, I think its clearly now.....

The FilteredBindingList cannot perform sorting unless it has been constructed from a SortedBindingList.

Cheers

ajj3085 replied on Wednesday, February 07, 2007

RockfordLhotka:
I always delegate the call - relying on the underlying implementation to throw any exceptions - unless the original list isn't an IBindingList, in which case I throw an exception directly.


Right... I didn't necessarly mean Csla code, just that at some point something would / should.

Copyright (c) Marimer LLC