Download the snippet at the end of this post.
To Install Snippet:
To use the snippet:
- Either right click and click Insert Snippet
- OR type BSBinding<tab>
- Replace the class name with your class name i.e. Employees
- Replace the object name with your object name i.e. Employee
Additional Requirements:
- You will need my BroncoSolutions.Components.dll to use the snippet as is
The result is the following:
[code language="VB.NET"]
Imports System.ComponentModel
Imports System.Reflection
Imports BroncoSolutions.Components.Windows.Collections
#Region " Classname "
Public Class Classname
Inherits System.ComponentModel.BindingList(Of ObjectName)
Private _Sorted As Boolean = False
Private _SortDirection As ListSortDirection
Private _SortProperty As PropertyDescriptor
Protected Overrides Function AddNewCore() As Object
Dim e As New ObjectName()
Add(e)
Return e
End Function
#Region "Sorting Support"
Protected Overrides Sub ApplySortCore(ByVal prop As System.ComponentModel.PropertyDescriptor, _
ByVal direction As System.ComponentModel.ListSortDirection)
' Get list to sort
Dim items As List(Of ObjectName) = TryCast(Me.Items, List(Of ObjectName))
' Apply and set the sort, if items to sort
If items IsNot Nothing Then
_SortDirection = direction
_SortProperty = prop
Dim pc As PropertyComparer(Of ObjectName) = _
New PropertyComparer(Of ObjectName)(prop, direction)
items.Sort(pc)
_Sorted = True
Else
_Sorted = False
End If
Me.OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
End Sub
Protected Overrides ReadOnly Property SupportsSortingCore() As Boolean
Get
Return True
End Get
End Property
Protected Overrides ReadOnly Property IsSortedCore() As Boolean
Get
Return _Sorted
End Get
End Property
Protected Overrides Sub RemoveSortCore()
_Sorted = False
End Sub
Protected Overrides ReadOnly Property SortDirectionCore() As System.ComponentModel.ListSortDirection
Get
Return _SortDirection
End Get
End Property
Protected Overrides ReadOnly Property SortPropertyCore() As System.ComponentModel.PropertyDescriptor
Get
Return _SortProperty
End Get
End Property
Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
Get
Return True
End Get
End Property
Protected Overrides Function FindCore(ByVal prop As System.ComponentModel.PropertyDescriptor, ByVal key As Object) As Integer
Dim items As List(Of ObjectName) = Me.Items
For Each item As ObjectName In items
If key = prop.GetValue(item) Then
Return IndexOf(item)
End If
Next
Return -1
End Function
#End Region
End Class
#End Region
[/code]