IBindingLsitView a simple implementation

IBindingLsitView a simple implementation

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


MatDavena posted on Tuesday, October 31, 2006

Hi Evertybody,

I’m relatively new  to CSLA. I think it’s a great framework and I choose to adopt it for my current project. I appreciate the Rocky’ SortedBindingList and FilteredBindingList, but in my project I have the need to give the user more flexibility on the presented data. That is the user could sort the information on multiple properties and dinamically filter the data basing on some quick selection  So I provide a simple implementation of  the IBindingListView interface, trying to save Rocky’s work as match as possible. The new class is BindingListView<T>.

 

View on data

Like the Rocky’s classes, the BindingListView do not create a copy of the data, but just a view on data, represented by a collection of ListItem object. The ListItem class has the responsibility to hold a reference to the original item, to hold the original position in the source list, and to cache values needed for the sorting process.

The view (ListItem collecion)  is created at the instantiation time and updated in only two cases: the filter is changed, the source list is changed.

When an item is added in the source list, the corresponding ListItem is simply appended to the view.

When an item is deleted from the source list, the corresponding ListItem is deleted.

When an item is changed, the corresponding ListItem is refreshed.

 

Sorting

For sorting I wrote a modified version of the Brian Noyes’ SortComparer (from his great book: Data Binding with Windows Forms 2.0). We can filter the list programmatically by setting the Sort property of the BindingList like the following sample:

 

CustomerList list = CustomerList.GetCustomerList(criteria);

BindingListView<Customer> view = new BindingListView<Customer>(list);

view.Sort = “Name ASC, Age DESC”;

 

Filtering

For the filtering process I used the same Predicate approach of the Jesse Johnston implementation ot IBindingListView (look at http://www.teamjohnston.net/cs/blogs/jesse for more details). For each filter condition is instantiated a Predicate class with a method (the predicate delegate) that evaluate whether a specified item instance meets the filter criteria. I created a different class for each of the primitive data types to avoid unnecessary boxing/unboxing operations. To filter the view we have to pass a filter string to the BindingListView class. The filter string the class accept are quite simple, because I have not the time to write a powerfull parser. We can write a filter like the following:

 

CustomerList list = CustomerList.GetCustomerList(criteria);

BindingListView<Customer> view = new BindingListView<Customer>(list);

view.Filter = “Name = ‘John’ AND Age = 32”;

 

The conditions on the different properties must be in AND. For string we can use the metacharacters “*” or “%” to simulate a Like condition, or we can specify a Regular Expression in this form:

 

CustomerList list = CustomerList.GetCustomerList(criteria);

BindingListView<Customer> view = new BindingListView<Customer>(list);

view.Filter = “Phone = Regex(“\b\d{3}-3434) AND Age=32”;

 

If you want it, you can download the source code from my personal web space: http://www.webalice.it/matteo.davena/en/BindingListView.html

 

I will appreciate your feedback.

Many thanks,

Matteo D’Avena

Bayu replied on Tuesday, October 31, 2006

Now that's a hell of a first post dude!

Great work!
Welcome to the forum!

Bayu


PS: too bad I have no immediate use for your work. Using Infragistics' stuff I have all these kinds of needs fulfilled already.

Brian Criswell replied on Tuesday, October 31, 2006

Welcome to the boards!  I will definitely be taking a look at this to see if I can get any ideas for the ObjectListView.  You can find it as part of the CSLAcontrib project.

MatDavena replied on Tuesday, October 31, 2006

Thanks guys for your reply!

brembot replied on Wednesday, December 20, 2006

Hello Mat,

I have used your BindingListView and it's very easy to implement. I think it's better if you create a powerful parser for the filters.

Thanks

DansDreams replied on Thursday, December 21, 2006

I've found the DevExpress controls are more than happy to manage all that for me such that I haven't had a need for the sorted or filtered lists.  You can imagine the hours that saves me.  Just a thought for people considering using a 3rd party suite.

Also, I expect Rocky's going to support LINQ in CSLA 3.0, and it's probably wise to start thinking that way.  I think that's the general direction Matteo has taken.  The real key I think is being careful where you put the criteria parsing that was suggested.  I know zero about linq other than basically what it is, but at this point anything I designed for filtering would take a strong consideration of what's coming from Microsoft.

Copyright (c) Marimer LLC