Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Sort and Filter Strongly-Typed Collection Classes

Earlier this week, I went looking for a way to filter Entity Collection classes.  I ran across a good article, Sorting the Unsortable Collection by Rocky Lhotka that details a VB wrapper class allowing sorting of strongly-typed collections. 

I liked the method that he presented, and created a similar called CollectionView that adds a feature for filtering. You can grab the class source for CollectionView here.  Using this class is easy.  Say you want to bind your strongly typed collection to a DataGrid, and enable sorting and filtering.  Simply pass your collection class to the constructor of the CollectionView, and bind your grid to the CollectionView instance instead.

CollectionView view = new CollectionView(MyCollectionClass);
this.DataGrid1.DataSource = view;
this.DataGrid1.DataBind();

Now, if you're using WinForms, you shouldn't have to do anything else to get Sorting to work, but if you're using the ASP.NET DataGrid, you have to wire up your Sort events.  To actually do the sort, use code like this:

view.ApplySort(e.SortExpression, System.ComponentModel.ListSortDirection.Ascending);

I implemented some simple code for filtering, it's admittedly limited, but it gets the job done for simple filters.   You should be able to expand this for multiple filters, by overloading the ApplyFilter method to accept an ArrayList of filters or even better, a strongly typed collection of filter objects or something like that.  But for simple filtering, with equals comparisons, just do the following:

view.ApplyFilter("PropertyName", propertyComparison);

Where property comparison is the object to compare against.  There's an overload too for NotEquals comparisons.  Anyhow hope this will simplify some aspects of DataBinding strongly typed collections for you.

-Brendan


Posted 09-02-2004 7:24 AM by Brendan Tompkins
Filed under:

[Advertisement]

Comments

A general way to sort a strong typed collection wrote re: Sort and Filter Strongly-Typed Collection Classes
on 09-23-2004 8:37 AM
Hi Brendan,

In the url section you will find a post I sent a while ago in the c# NG, there I posted the code I use to sort any strong typed collection using reflection,
It can be used to sort based on any property, even complex one ie: Person.Address.Zip

Please if you post it or modify it in any way just give credit ;)

Cheers,
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Brendan Tompkins wrote re: Sort and Filter Strongly-Typed Collection Classes
on 09-23-2004 8:49 AM
Xurxo F wrote re: Sort and Filter Strongly-Typed Collection Classes
on 09-28-2004 5:45 AM
I'm interested in Ignacio's sample, but URL is break. Sorry for my english.
hodge wrote re: Sort and Filter Strongly-Typed Collection Classes
on 10-10-2004 6:48 AM
I would kiss you if I could. This is great stuff.
Bruce wrote re: Sort and Filter Strongly-Typed Collection Classes
on 12-14-2004 4:04 PM
Hi, Great class, but it had me trapped for a while on the "PropertyName" in ApplyFilter. It is CASE sensitive. I flip too much between C# and Vb.Net and disnt tweak to it for a while :-(

Thanks for the great post.
dino wrote re: Sort and Filter Strongly-Typed Collection Classes
on 12-16-2004 9:54 AM
I'm not kissing you, but still, this is quite nice. thanks.

Also - if users of this thing want to apply a DataGridTableStyle, use "CollectionView" as the MappingName. (for a generic ArrayList, use "ArrayList").
dino wrote One more thing
on 12-16-2004 12:40 PM
The RemoveFilter() method, I think, should fire the ListChange() event.
Brendan Tompkins wrote re: Sort and Filter Strongly-Typed Collection Classes
on 12-16-2004 1:13 PM
Dino. Thanks. It's okay about the kiss too. :)
dino wrote re: Sort and Filter Strongly-Typed Collection Classes
on 01-06-2005 5:24 AM
ps, I changed a few things:
- support for "Includes" and "NotIncludes" filters in addition to the "Equals" and NotEquals filters
- better support sorting and filtering at the same time. Eg, when removing a filter, need to re-sort.
- caching of property descriptors

find the modified version at
http://cheeso.members.winisp.net/srcview.aspx?dir=misc&file=CollectionView.cs
Brendan Tompkins wrote re: Sort and Filter Strongly-Typed Collection Classes
on 01-06-2005 5:37 AM
Cool! Thanks, Dino.

Brendan
TSHAK wrote User Defined Sorting and Filtering Strongly Typed Collections
on 01-08-2005 9:32 AM
User Defined Sorting and Filtering Strongly Typed Collections
Dotnet Technologies wrote Filtering Collection Classes
on 01-14-2005 1:57 PM
TrackBack wrote A Collection of Collections Stuff
on 02-08-2005 9:12 AM
Zac Glenn wrote re: Sort and Filter Strongly-Typed Collection Classes
on 02-16-2005 8:56 AM
Thank You! Thank You!.... I been looking for this for a while.
TrackBack wrote re: Implementing IComparable for Sorting Custom Objects
on 02-27-2005 1:57 PM
TrackBack wrote sorting/filtering strongly typed collections
on 03-16-2005 2:12 AM
sorting/filtering strongly typed collections
TrackBack wrote Sort/Filter Your Business Entity Collections with a NEW CollectionView
on 03-18-2005 11:11 AM
Brendan Tompkins wrote Sort/Filter Your Business Entity Collections with a NEW CollectionView
on 03-23-2005 12:40 PM
You’ve probably heard a lot of talk about why you should
use Business Entities rather than...
Brendan Tompkins wrote Sort/Filter Your Business Entity Collections with a NEW CollectionView
on 07-25-2005 12:05 PM
You’ve probably heard a lot of talk about why you should
use Business Entities rather than...
Richard Slade wrote re: Sort and Filter Strongly-Typed Collection Classes
on 10-22-2005 10:45 AM
Hi,

I wrote something similar before I found your filter. The one I have written looks similar in functionality. Overloads allow AND/OR operations as well as Equals and NotEqualTo.

http://www.dotnetjunkies.com/WebLog/richardslade/articles/category/2680.aspx

Cheers

R
mitch wrote re: Sort and Filter Strongly-Typed Collection Classes
on 03-12-2006 7:38 PM
The comments on this blog would probably classify for some "Programmers Gone Wild!" shorts ;)

This is good stuff though, thank you!
Bill wrote re: Sort and Filter Strongly-Typed Collection Classes
on 05-28-2006 11:38 PM
Great (best practice) code and wonderful example of programmers building on one another starting with "sorting the unsortable" and continuing with dino (and others).  I don't have a url, but have vb.net version if interested (I'll check back). Thank you for paving the way.  
Chris wrote re: Sort and Filter Strongly-Typed Collection Classes
on 06-02-2006 3:51 PM
This is great stuff!

I'd definitely like to see Bill's VB.net version!!
Stormy wrote re: Sort and Filter Strongly-Typed Collection Classes
on 06-05-2006 9:48 PM
Can I add a check box in a column by using CollectionView? Thanks
Bilal Haidar [MVP] :: Blog wrote Filter Collection of Objects
on 10-05-2006 12:30 AM

I have been developing an API for an application of mine at work. The API returns a set of objects in...

Eric Rockenbach wrote re: Sort and Filter Strongly-Typed Collection Classes
on 12-15-2006 2:12 PM

Imports System

Imports System.Collections

Imports System.ComponentModel

Imports System.Reflection

<Serializable()> _

Public Class EtracCollection

   Implements IBindingList

   Implements IComponent

   Implements IDisposable

   Public Event ListChanged As System.ComponentModel.ListChangedEventHandler Implements System.ComponentModel.IBindingList.ListChanged

   Public Sub New(ByVal list As IList)

       _baseList = list

       If TypeOf BaseList Is IBindingList Then

           _supportsBinding = True

           _bindingList = CType(BaseList, IBindingList)

           AddHandler _bindingList.ListChanged, AddressOf SourceChanged

       End If

   End Sub

   Public Sub ApplyDistinct(ByVal distinctField As String)

       If (Me._isFiltered) Then

           ApplyDistinct(Me._filteredList, distinctField)

       Else

           ApplyDistinct(Me.BaseList, distinctField)

       End If

       _isDistinct = True

   End Sub

   Private Sub ApplyDistinct(ByRef List As ArrayList, ByVal distinctField As String)

       If List.Count > 0 Then

           Dim count As Integer

           For count = 0 To List.Count - 1 Step 1

               If (List.Count > count) Then

                   Dim type As System.Type = List(count).GetType()

                   Dim Properties As System.Reflection.PropertyInfo() = type.GetProperties()

                   For Each prop As System.Reflection.PropertyInfo In Properties

                       If (List.Count > count) Then

                           Dim propNames As String = prop.Name()

                           Dim propValue As Object = prop.GetValue(List(count), Nothing)

                           If propNames.ToLower() = distinctField.ToLower() Then

                               'Verify Value is the same as the filer criteria otherwise remove the object from the array list

                               If _distinctPropertyList.IndexOf(propValue) > -1 Then

                                   List.RemoveAt(count)

                                   count = count - 1

                               Else

                                   _distinctPropertyList.Add(propValue)

                               End If

                           End If

                       End If

                   Next

               End If

           Next

       End If

   End Sub

   Public Function Add(ByVal value As Object) As Integer Implements System.Collections.IList.Add

       Return BaseList.Add(value)

   End Function

   Public Sub AddIndex(ByVal property1 As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.AddIndex

       If _supportsBinding Then

           _bindingList.AddIndex(property1)

       End If

   End Sub

   Public Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew

       If _supportsBinding Then

           Return _bindingList.AddNew

       End If

       Return Nothing

   End Function

   Public Sub ApplyFilter(ByVal property1 As String, ByVal compareValue As Object)

       Me.ApplyFilter(property1, compareValue, FilterOperand.Equals)

   End Sub

   Public Enum FilterOperand

       Equals

       NotEquals

   End Enum

   Public Sub ApplyFilter(ByVal property1 As String, ByVal compareValue As Object, ByVal operand As FilterOperand)

       If _baseList.Count > 0 Then

           Dim filterBy As PropertyDescriptor = GetPropertyDescriptor(property1)

           _filteredList = New ArrayList

           For Each obj As Object In _baseList

               Select Case operand

                   Case FilterOperand.Equals

                       If filterBy.GetValue(obj).Equals(compareValue) Then

                           _filteredList.Add(obj)

                       End If

                       ' break

                   Case FilterOperand.NotEquals

                       If Not (filterBy.GetValue(obj).Equals(compareValue)) Then

                           _filteredList.Add(obj)

                       End If

                       ' break

               End Select

           Next

           _isFiltered = True

           RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))

       End If

   End Sub

   Public Sub ApplyPaging(ByVal recordsPerPage As Integer, ByVal currentPage As Integer)

       _paging = True

       _currentPage = currentPage

       _RecordsPerPage = recordsPerPage

   End Sub

   Private Sub PageCurrectList(ByRef list As ArrayList)

       _paging = False

       _pagedList = New ArrayList

       Dim count As Integer

       Dim index As Integer = 0

       If list.Count > _RecordsPerPage Then

           If list.Count > (_RecordsPerPage * (_currentPage - 1)) Then

               If CurrentPage > 1 Then

                   index = (_RecordsPerPage * (_currentPage - 1))

               End If

               For count = 0 To _RecordsPerPage - 1 Step 1

                   If list.Count > (index + count) Then

                       Me._pagedList.Add(list((index + count)))

                   End If

               Next

           Else

               'No Records on page

               _pagedList.Clear()

           End If

       Else

           For count = 0 To _RecordsPerPage - 1 Step 1

               If list.Item((index + count)) <> Nothing Then

                   Me._pagedList.Add(list.Item((index + count)))

               End If

           Next

       End If

       _paging = True

   End Sub

   Public Sub ApplySort(ByVal property1 As String, ByVal direction As System.ComponentModel.ListSortDirection)

       If _baseList.Count > 0 Then

           _sortBy = GetPropertyDescriptor(property1)

           _sortDirection = direction

           DoSort()

           UpdateBaseListOnSort()

       End If

   End Sub

   Public Sub UpdateBaseListOnSort()

       _baseList.Clear()

       Dim icount As Integer

       For icount = 0 To Me._sortedList.Count - 1 Step 1

           _baseList.Add(Me.GetSortedItem(icount))

       Next

   End Sub

   Public Sub Clear() Implements System.Collections.IList.Clear

       BaseList.Clear()

   End Sub

   Public Function Contains(ByVal value As Object) As Boolean Implements System.Collections.IList.Contains

       Return BaseList.Contains(value)

   End Function

   Public Sub CopyTo(ByVal array As System.Array, ByVal index As Integer) Implements System.Collections.ICollection.CopyTo

       BaseList.CopyTo(array, index)

   End Sub

   Public Function Find(ByVal property1 As System.ComponentModel.PropertyDescriptor, ByVal key As Object) As Integer Implements System.ComponentModel.IBindingList.Find

       If _supportsBinding Then

           Return _bindingList.Find(property1, key)

       Else

           Return -1

       End If

   End Function

   Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator

       If IsSorted Then

           Return New SortedEnumerator(_sortedList, SortDirection)

       Else

           Return BaseList.GetEnumerator

       End If

   End Function

   Public Function IndexOf(ByVal value As Object) As Integer Implements System.Collections.IList.IndexOf

       If _isSorted Then

           Dim index As Integer = 0

           For Each item As ListItem In _sortedList

               If item.Item.Equals(value) Then

                   Return index

               End If

               index += 1

           Next

           Return -1

       Else

           Return BaseList.IndexOf(value)

       End If

   End Function

   Public Sub Insert(ByVal index As Integer, ByVal value As Object) Implements System.Collections.IList.Insert

       BaseList.Insert(index, value)

   End Sub

   Public Sub Remove(ByVal value As Object) Implements System.Collections.IList.Remove

       BaseList.Remove(value)

   End Sub

   Public Sub RemoveAt(ByVal index As Integer) Implements System.Collections.IList.RemoveAt

       If IsSorted Then

           Dim pos As Integer = BaseList.IndexOf(GetSortedItem(index))

           BaseList.RemoveAt(pos)

       Else

           BaseList.RemoveAt(index)

       End If

   End Sub

   Public Sub RemoveFilter()

       _filteredList = Nothing

       _isFiltered = False

   End Sub

   Public Sub RemoveIndex(ByVal property1 As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.RemoveIndex

       If _supportsBinding Then

           _bindingList.RemoveIndex(property1)

       End If

   End Sub

   Public Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort

       UndoSort()

   End Sub

   Public Property Paging() As Boolean

       Get

           Return Me._paging

       End Get

       Set(ByVal Value As Boolean)

           _paging = Value

       End Set

   End Property

   Public Property CurrentPage() As Integer

       Get

           Return _currentPage

       End Get

       Set(ByVal Value As Integer)

           _currentPage = Value

       End Set

   End Property

   Public Property RecordsPerPage() As Integer

       Get

           Return Me._RecordsPerPage

       End Get

       Set(ByVal Value As Integer)

           Me._RecordsPerPage = Value

       End Set

   End Property

   Public ReadOnly Property AllowEdit() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit

       Get

           If _supportsBinding Then

               Return _bindingList.AllowEdit

           Else

               Return False

           End If

       End Get

   End Property

   Public ReadOnly Property AllowNew() As Boolean Implements System.ComponentModel.IBindingList.AllowNew

       Get

           If _supportsBinding Then

               Return _bindingList.AllowNew

           Else

               Return False

           End If

       End Get

   End Property

   Public ReadOnly Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove

       Get

           If _supportsBinding Then

               Return _bindingList.AllowRemove

           Else

               Return False

           End If

       End Get

   End Property

   Public ReadOnly Property BaseList() As IList

       Get

           If ((_isFiltered) And (_paging)) Then

               PageCurrectList(_filteredList)

               Return _pagedList

           ElseIf (_isFiltered) Then

               Return _filteredList

           ElseIf (_paging) Then

               PageCurrectList(_baseList)

               Return _pagedList

           Else

               Return _baseList

           End If

       End Get

   End Property

   Public ReadOnly Property Count() As Integer Implements System.Collections.ICollection.Count

       Get

           Return BaseList.Count

       End Get

   End Property

   Public ReadOnly Property IsFixedSize() As Boolean Implements System.Collections.IList.IsFixedSize

       Get

           Return BaseList.IsFixedSize

       End Get

   End Property

   Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.IList.IsReadOnly

       Get

           Return True

       End Get

   End Property

   Public ReadOnly Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted

       Get

           Return IsSorted

       End Get

   End Property

   Public ReadOnly Property IsSynchronized() As Boolean Implements System.Collections.ICollection.IsSynchronized

       Get

           Return BaseList.IsSynchronized

       End Get

   End Property

   Public ReadOnly Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection

       Get

           Return SortDirection

       End Get

   End Property

   Public ReadOnly Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty

       Get

           Return _sortBy

       End Get

   End Property

   Public ReadOnly Property SupportsChangeNotification() As Boolean Implements System.ComponentModel.IBindingList.SupportsChangeNotification

       Get

           Return True

       End Get

   End Property

   Public ReadOnly Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching

       Get

           If _supportsBinding Then

               Return _bindingList.SupportsSearching

           Else

               Return False

           End If

       End Get

   End Property

   Public ReadOnly Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting

       Get

           Return True

       End Get

   End Property

   Public ReadOnly Property SyncRoot() As Object Implements System.Collections.ICollection.SyncRoot

       Get

           Return BaseList.SyncRoot

       End Get

   End Property

   Default Public Property Blubber(ByVal index As Integer) As Object Implements System.Collections.IList.Item

       Get

           If IsSorted Then

               Return GetSortedItem(index)

           Else

               Return BaseList(index)

           End If

       End Get

       Set(ByVal Value As Object)

           If IsSorted Then

               Dim pos As Integer = BaseList.IndexOf(GetSortedItem(index))

               BaseList(pos) = Value

               If Not _supportsBinding Then

                   DoSort()

               End If

           Else

               BaseList(index) = Value

           End If

       End Set

   End Property

   Private _baseList As IList

   Private _bindingList As IBindingList

   Private _filteredList As ArrayList

   Private _isFiltered As Boolean = False

   Private _isSorted As Boolean = False

   Private _sortBy As PropertyDescriptor

   Private _sortDirection As ListSortDirection = ListSortDirection.Ascending

   Private _sortedList As ArrayList = New ArrayList

   Private _supportsBinding As Boolean

   Private _distinctPropertyList As ArrayList = New ArrayList

   Private _isDistinct As Boolean = False

   Private _paging As Boolean = False

   Private _currentPage As Integer = 1

   Private _RecordsPerPage As Integer = 12

   Private _pagedList As ArrayList

   Private Sub DoSort()

       _sortedList.Clear()

       If _sortBy Is Nothing Then

           For Each obj As Object In BaseList

               _sortedList.Add(New ListItem(obj, obj))

           Next

       Else

           For Each obj As Object In BaseList

               _sortedList.Add(New ListItem(_sortBy.GetValue(obj), obj))

           Next

       End If

       _sortedList.Sort()

       _isSorted = True

       RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))

   End Sub

   Private Function GetPropertyDescriptor(ByVal property1 As String) As PropertyDescriptor

       Dim itemType As Type = Nothing

       If property1.Length > 0 Then

           Dim t As Type = Nothing

           If _baseList.Count > 0 Then

               t = _baseList(0).GetType

           End If

           'Dim t As Type = GetType(ExxonMobil.ETrac.DataObjects.CityInfo)

           Dim defs As MemberInfo() = t.GetDefaultMembers

           For Each member As MemberInfo In defs

               If member.MemberType = MemberTypes.Property Then

                   itemType = CType(member, PropertyInfo).GetGetMethod.ReturnType

                   ' break

               End If

           Next

           If itemType Is Nothing Then

               If _baseList.Count > 0 Then

                   itemType = _baseList(0).GetType

               Else

                   Throw New Exception("Can not determine collection item type")

               End If

           End If

           Dim props As PropertyDescriptorCollection

           props = TypeDescriptor.GetProperties(itemType)

           For Each prop As PropertyDescriptor In props

               If prop.Name = property1 Then

                   Return prop

               End If

           Next

       End If

       Return Nothing

   End Function

   Private Function GetSortedItem(ByVal index As Integer) As Object

       If SortDirection = ListSortDirection.Ascending Then

           Return CType(_sortedList(index), ListItem).Item

       Else

           Return CType(_sortedList(_sortedList.Count - 1 - index), ListItem).Item

       End If

   End Function

   Private Sub SourceChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs)

       If _isSorted Then

           If e.ListChangedType = ListChangedType.ItemAdded Then

               If _sortDirection = ListSortDirection.Ascending Then

                   _sortedList.Add(New ListItem(_sortBy.GetValue(BaseList(e.NewIndex)), _baseList(e.NewIndex)))

               Else

                   _sortedList.Insert(0, New ListItem(_sortBy.GetValue(BaseList(e.NewIndex)), _baseList(e.NewIndex)))

               End If

               RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemAdded, _sortedList.Count - 1))

           Else

               DoSort()

           End If

       Else

           RaiseEvent ListChanged(Me, e)

       End If

   End Sub

   Private Sub UndoSort()

       _sortedList.Clear()

       _sortBy = Nothing

       _sortDirection = ListSortDirection.Ascending

       _isSorted = False

       RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))

   End Sub

   Private Class ListItem

       Implements IComparable

       Public Item As Object

       Public Key As Object

       Public Sub New(ByVal key As Object, ByVal item As Object)

           Me.Key = key

           Me.Item = item

       End Sub

       Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

           Dim target As Object = CType(obj, ListItem).Key

           If TypeOf Key Is IComparable Then

               Return CType(Key, IComparable).CompareTo(target)

           Else

               If Key.Equals(target) Then

                   Return 0

               Else

                   Return Key.ToString.CompareTo(target.ToString)

               End If

           End If

       End Function

       Public Overloads Overrides Function ToString() As String

           Return Key.ToString

       End Function

   End Class

   Private Class SortedEnumerator

       Implements IEnumerator

       Private index As Integer

       Private mSortIndex As ArrayList

       Private mSortOrder As ListSortDirection

       Public Sub New(ByVal sortIndex As ArrayList, ByVal direction As ListSortDirection)

           mSortIndex = sortIndex

           mSortOrder = direction

           Reset()

       End Sub

       Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext

           If mSortOrder = ListSortDirection.Ascending Then

               If index < mSortIndex.Count - 1 Then

                   index += 1

                   Return True

               Else

                   Return False

               End If

           Else

               If index > 0 Then

                   index -= 1

                   Return True

               Else

                   Return False

               End If

           End If

       End Function

       Public Sub Reset() Implements IEnumerator.Reset

           If mSortOrder = ListSortDirection.Ascending Then

               index = -1

           Else

               index = mSortIndex.Count

           End If

       End Sub

       Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current

           Get

               Return CType(mSortIndex(index), ListItem).Item

           End Get

       End Property

   End Class

   Public Property Site() As ISite Implements System.ComponentModel.IComponent.Site

       Get

           Return _site

       End Get

       Set(ByVal Value As ISite)

           _site = Value

       End Set

   End Property

   Private _site As ISite = Nothing

   Public Sub Dispose() Implements System.IDisposable.Dispose

       RaiseEvent Disposed(Me, EventArgs.Empty)

   End Sub

   Public Event Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.ComponentModel.IComponent.Disposed

   Public Sub ApplySort(ByVal property1 As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort

   End Sub

End Class

Eric Rockenbach wrote re: Sort and Filter Strongly-Typed Collection Classes
on 12-15-2006 2:17 PM

Sorry for the long post guys I didn't have access to an FTP at the current time, but I felt it was only right for me to post the VB.NET version since I used the C# verbatim.  I added a few additional items including support for paging and distinct clauses as sometimes I use this for when I have little to no control  over the db and I come in and "save" a project in the last few weeks these items proove helpfull.

Thanks again for the oringal post!  

HyVong wrote re: Sort and Filter Strongly-Typed Collection Classes
on 07-16-2007 2:57 PM

As an unexperienced developer, i get excited when i find some sample code and hope to be able to learn a few things from the sample code.  I wish there were comments in the code provided above by Eric because i am having difficulty follow the example :( .  Is it possible if i can have an example and how you would apply the above code.  If i want to apply this to a my collection, how would i implement it?  Any sample pages would be very apprciated.

Richard Reedy wrote re: Sort and Filter Strongly-Typed Collection Classes
on 08-19-2008 3:10 PM

Just wanted to say that this helps alot, thanks for the post.

and Thanks Dino for the modifications to the original class.

DG wrote re: Sort and Filter Strongly-Typed Collection Classes
on 09-23-2008 12:38 PM

When I included the class into my simple project, things worked initially meaning the CollectionView object had nicely bound to my grid. But when I did the sort, the Gridview_Sorting event fired and the view object that I had bound to the grid was obviously now unaccessable.  This meant that I couldnt do the "ApplySort".

So as an initial test, I created a viewstate variable to hold a CollectionView object for the postback occurring during the sort.  Unfortunately, I received the following error:

Type 'System.ComponentModel.PropertyDescriptor' in Assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

I've tinkered with it a little but havent been able to get things working.  Any ideas?

Ronald wrote re: Sort and Filter Strongly-Typed Collection Classes
on 10-13-2008 9:17 AM

Hi,

thak you fpor that nice class. But i have a problem. When i bind a new CollectionView to an DataGridView  i can't edit the content. The grid and the colums are readonly = false. The Collectionview is alos readonly = false. Does anyone know why i can't edit the content?

Thanks

Sortable, Bindable Object Collection? | keyongtech wrote Sortable, Bindable Object Collection? | keyongtech
on 01-22-2009 3:03 AM

Pingback from  Sortable, Bindable Object Collection? | keyongtech

Add a Comment

(required)  
(optional)
(required)  
Remember Me?