实体类实现了IEditableObject接口,IEditableObject接口的作用是在绑定后可以按Esc取消先前输入的值.
''' -----------------------------------------------------------------------------
''' Project : 实体和实体集合的绑定
''' Class : Customer
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 实体类.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CustomerClass Customer
Implements System.ComponentModel.IEditableObject
成员变量#Region "成员变量"
Private md_id As Integer
Private md_name As String = String.Empty
Private md_age As Integer
Private md_isNew As Boolean = True
Private md_isEdit As Boolean = False
Private md_editCustomer As Customer
#End Region
类实例化#Region "类实例化"
Public Sub New()Sub New()
End Sub
Public Sub New()Sub New(ByVal id As Integer)
Me.New()
Me.ID = id
End Sub
#End Region
事件声明#Region "事件声明"
Public Event RemoveMe(ByVal customer As Customer)
#End Region
实体属性#Region "实体属性"
Public Property ID()Property ID() As Integer
Get
Return Me.md_id
End Get
Set(ByVal Value As Integer)
Me.md_id = Value
End Set
End Property
Public Property Name()Property Name() As String
Get
Return Me.md_name
End Get
Set(ByVal Value As String)
Me.md_name = Value
End Set
End Property
Public Property Age()Property Age() As String
Get
Return Me.md_age
End Get
Set(ByVal Value As String)
Me.md_age = Value
End Set
End Property
#End Region
IEditableObject#Region "IEditableObject"
''' -----------------------------------------------------------------------------
''' <summary>
''' 开始编辑.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub BeginEdit()Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
If Me.md_isNew Then
Else
If Not Me.md_isEdit Then
'处于编辑的标志
Me.md_isEdit = True
Me.md_editCustomer = New Customer
'保存起来.便于在执行CancelEdit方法时恢复.
Me.md_editCustomer.Name = Me.Name
Me.md_editCustomer.ID = Me.ID
Me.md_editCustomer.Age = Me.Age
End If
End If
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 取消编辑.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub CancelEdit()Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
If Me.md_isNew Then
RaiseEvent RemoveMe(Me)
Else
'判断是否处于编辑模式
If Me.md_isEdit Then
Me.Age = Me.md_editCustomer.Age
Me.ID = Me.md_editCustomer.ID
Me.Name = Me.md_editCustomer.Name
End If
End If
Me.md_isEdit = False
Me.md_isNew = False
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 结束编辑.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub EndEdit()Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
Me.md_isNew = False
Me.md_isEdit = False
End Sub
#End Region
End Class
''' Project : 实体和实体集合的绑定
''' Class : Customer
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 实体类.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CustomerClass Customer
Implements System.ComponentModel.IEditableObject
成员变量#Region "成员变量"
Private md_id As Integer
Private md_name As String = String.Empty
Private md_age As Integer
Private md_isNew As Boolean = True
Private md_isEdit As Boolean = False
Private md_editCustomer As Customer
#End Region
类实例化#Region "类实例化"
Public Sub New()Sub New()
End Sub
Public Sub New()Sub New(ByVal id As Integer)
Me.New()
Me.ID = id
End Sub
#End Region
事件声明#Region "事件声明"
Public Event RemoveMe(ByVal customer As Customer)
#End Region
实体属性#Region "实体属性"
Public Property ID()Property ID() As Integer
Get
Return Me.md_id
End Get
Set(ByVal Value As Integer)
Me.md_id = Value
End Set
End Property
Public Property Name()Property Name() As String
Get
Return Me.md_name
End Get
Set(ByVal Value As String)
Me.md_name = Value
End Set
End Property
Public Property Age()Property Age() As String
Get
Return Me.md_age
End Get
Set(ByVal Value As String)
Me.md_age = Value
End Set
End Property
#End Region
IEditableObject#Region "IEditableObject"
''' -----------------------------------------------------------------------------
''' <summary>
''' 开始编辑.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub BeginEdit()Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
If Me.md_isNew Then
Else
If Not Me.md_isEdit Then
'处于编辑的标志
Me.md_isEdit = True
Me.md_editCustomer = New Customer
'保存起来.便于在执行CancelEdit方法时恢复.
Me.md_editCustomer.Name = Me.Name
Me.md_editCustomer.ID = Me.ID
Me.md_editCustomer.Age = Me.Age
End If
End If
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 取消编辑.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub CancelEdit()Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
If Me.md_isNew Then
RaiseEvent RemoveMe(Me)
Else
'判断是否处于编辑模式
If Me.md_isEdit Then
Me.Age = Me.md_editCustomer.Age
Me.ID = Me.md_editCustomer.ID
Me.Name = Me.md_editCustomer.Name
End If
End If
Me.md_isEdit = False
Me.md_isNew = False
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 结束编辑.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub EndEdit()Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
Me.md_isNew = False
Me.md_isEdit = False
End Sub
#End Region
End Class
实体集合主要实现了ITypedList接口,主要作用是用于可以自定义可绑定的实体属性.IBindingList接口主要是用于绑定后是否允许编辑,是否允许删除,是否允许添加,排序等丰富的绑定属性定义.IComparer对排序的支持.
实体集合
Imports System.ComponentModel
''' -----------------------------------------------------------------------------
''' Project : 实体和实体集合的绑定
''' Class : CustomersList
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 实体集合.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CustomersListClass CustomersList
Inherits CollectionBase
Implements System.ComponentModel.ITypedList, System.ComponentModel.IBindingList, System.Collections.IComparer
'排序的列
Private md_SortName As String = String.Empty
'排序方式
Private md_SortDir As System.ComponentModel.ListSortDirection = ListSortDirection.Ascending
ITypedList#Region "ITypedList"
''' -----------------------------------------------------------------------------
''' <summary>
''' 在实际中,可以控制实体的那些属性可以进行绑定.
''' </summary>
''' <param name="listAccessors"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Function GetItemProperties()Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties
Dim pd(2) As PropertyDescriptor
Dim c As New Customer
'反射是对大小敏感的.
pd(0) = New CustomPropertyDescriptor("ID", c.GetType.GetProperty("ID"))
pd(1) = New CustomPropertyDescriptor("Age", c.GetType.GetProperty("Age"))
pd(2) = New CustomPropertyDescriptor("Name", c.GetType.GetProperty("Name"))
Return New System.ComponentModel.PropertyDescriptorCollection(pd)
End Function
Public Function GetListName()Function GetListName(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As String Implements System.ComponentModel.ITypedList.GetListName
Return ""
End Function
#End Region
IBindingList#Region "IBindingList"
Private Sub RemoveMe()Sub RemoveMe(ByVal c As Customer)
Me.List.Remove(c)
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 引发集合项发生改变时的事件.
''' </summary>
''' <param name="e"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overridable Sub OnListChanged()Sub OnListChanged(ByVal e As System.ComponentModel.ListChangedEventArgs)
RaiseEvent ListChanged(Me, e)
End Sub
Private Property Sort()Property Sort() As String
Get
Return Me.md_SortName
End Get
Set(ByVal Value As String)
md_SortName = Value
If md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
Me.md_SortName = String.Empty
Else
Me.InnerList.Sort(Me)
Me.OnListChanged(New System.ComponentModel.ListChangedEventArgs(ListChangedType.Reset, 0))
End If
End Set
End Property
Public Sub AddIndex()Sub AddIndex(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.AddIndex
End Sub
Public Function AddNew()Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew
Dim c As New Customer
AddHandler c.RemoveMe, AddressOf RemoveMe '用于在绑定后编辑时,取消编辑时从集合中移除.
Me.List.Add(c)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemAdded, List.Count - 1))
Return c
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 绑定后是否允许编辑.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property AllowEdit()Property AllowEdit() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit
Get
Return True
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 绑定后是否允许添加
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property AllowNew()Property AllowNew() As Boolean Implements System.ComponentModel.IBindingList.AllowNew
Get
Return True
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 绑定后是否允许删除
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property AllowRemove()Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove
Get
Return True
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 执行排序.
''' </summary>
''' <param name="property"></param>
''' <param name="direction"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub ApplySort()Sub ApplySort(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort
Me.md_SortDir = direction
Me.Sort = [Property ]()property].DisplayName
End Sub
Public Function Find()Function Find(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor, ByVal key As Object) As Integer Implements System.ComponentModel.IBindingList.Find
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 是否正处于排序模式.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property IsSorted()Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted
Get
If Me.md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
Return False
Else
Return True
End If
End Get
End Property
Public Event ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Implements System.ComponentModel.IBindingList.ListChanged
Public Sub RemoveIndex()Sub RemoveIndex(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.RemoveIndex
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 取消排序
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub RemoveSort()Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort
Me.Sort = String.Empty
End Sub
Public ReadOnly Property SortDirection()Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection
Get
Return Me.md_SortDir
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回正在的排序的列
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property SortProperty()Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty
Get
If Me.md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
Return Nothing
Else
Return New CustomPropertyDescriptor(Me.md_SortName, GetType(Customer).GetProperty(Me.md_SortName))
End If
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 更改通知.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property SupportsChangeNotification()Property SupportsChangeNotification() As Boolean Implements System.ComponentModel.IBindingList.SupportsChangeNotification
Get
Return True
End Get
End Property
Public ReadOnly Property SupportsSearching()Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching
Get
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 是否允许排序.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property SupportsSorting()Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting
Get
Return True
End Get
End Property
#End Region
IComparer#Region "IComparer"
Public Function Compare()Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim objX As Object
Dim objY As Object
Dim ReturnValue As Integer
If TypeOf x Is Customer Then
objX = CType(x, Customer).GetType.GetProperty(Me.md_SortName).GetValue(x, Nothing)
Else
objX = x
End If
If TypeOf y Is Customer Then
objY = CType(y, Customer).GetType.GetProperty(Me.md_SortName).GetValue(y, Nothing)
Else
objY = y
End If
If IsDBNull(objX) Then
If IsDBNull(objY) Then
ReturnValue = 0
Else
ReturnValue = -1
End If
Else
If IsDBNull(objY) Then
ReturnValue = 1
ElseIf objX > objY Then
ReturnValue = 1
ElseIf objX = objY Then
Return 0
ElseIf objX < objY Then
ReturnValue = -1
End If
End If
If Not Me.md_SortDir = ListSortDirection.Ascending Then ReturnValue = -ReturnValue
Return ReturnValue
End Function
#End Region
重写CollectionBase方法#Region "重写CollectionBase方法"
''' -----------------------------------------------------------------------------
''' <summary>
''' 清除集合完成后.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnClearComplete()Sub OnClearComplete()
OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, 0))
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 插入完成后.
''' </summary>
''' <param name="index"></param>
''' <param name="value"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnInsertComplete()Sub OnInsertComplete(ByVal index As Integer, ByVal value As Object)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemAdded, index))
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 移除完成后.
''' </summary>
''' <param name="index"></param>
''' <param name="value"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnRemoveComplete()Sub OnRemoveComplete(ByVal index As Integer, ByVal value As Object)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemDeleted, index))
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 改变集合中项目的值后.
''' </summary>
''' <param name="index"></param>
''' <param name="oldValue"></param>
''' <param name="newValue"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnSetComplete()Sub OnSetComplete(ByVal index As Integer, ByVal oldValue As Object, ByVal newValue As Object)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemChanged, index))
End Sub
#End Region
Public Function Add()Function Add(ByVal value As Customer) As Integer
Return Me.List.Add(value)
End Function
Public Sub Remove()Sub Remove(ByVal Value As Customer)
Me.List.Remove(Value)
End Sub
Public Property Item()Property Item(ByVal index As Integer) As Customer
Get
Return Me.List.Item(index)
End Get
Set(ByVal Value As Customer)
Me.List.Item(index) = Value
End Set
End Property
End Class
Imports System.ComponentModel
''' -----------------------------------------------------------------------------
''' Project : 实体和实体集合的绑定
''' Class : CustomersList
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 实体集合.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CustomersListClass CustomersList
Inherits CollectionBase
Implements System.ComponentModel.ITypedList, System.ComponentModel.IBindingList, System.Collections.IComparer
'排序的列
Private md_SortName As String = String.Empty
'排序方式
Private md_SortDir As System.ComponentModel.ListSortDirection = ListSortDirection.Ascending
ITypedList#Region "ITypedList"
''' -----------------------------------------------------------------------------
''' <summary>
''' 在实际中,可以控制实体的那些属性可以进行绑定.
''' </summary>
''' <param name="listAccessors"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Function GetItemProperties()Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties
Dim pd(2) As PropertyDescriptor
Dim c As New Customer
'反射是对大小敏感的.
pd(0) = New CustomPropertyDescriptor("ID", c.GetType.GetProperty("ID"))
pd(1) = New CustomPropertyDescriptor("Age", c.GetType.GetProperty("Age"))
pd(2) = New CustomPropertyDescriptor("Name", c.GetType.GetProperty("Name"))
Return New System.ComponentModel.PropertyDescriptorCollection(pd)
End Function
Public Function GetListName()Function GetListName(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As String Implements System.ComponentModel.ITypedList.GetListName
Return ""
End Function
#End Region
IBindingList#Region "IBindingList"
Private Sub RemoveMe()Sub RemoveMe(ByVal c As Customer)
Me.List.Remove(c)
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 引发集合项发生改变时的事件.
''' </summary>
''' <param name="e"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overridable Sub OnListChanged()Sub OnListChanged(ByVal e As System.ComponentModel.ListChangedEventArgs)
RaiseEvent ListChanged(Me, e)
End Sub
Private Property Sort()Property Sort() As String
Get
Return Me.md_SortName
End Get
Set(ByVal Value As String)
md_SortName = Value
If md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
Me.md_SortName = String.Empty
Else
Me.InnerList.Sort(Me)
Me.OnListChanged(New System.ComponentModel.ListChangedEventArgs(ListChangedType.Reset, 0))
End If
End Set
End Property
Public Sub AddIndex()Sub AddIndex(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.AddIndex
End Sub
Public Function AddNew()Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew
Dim c As New Customer
AddHandler c.RemoveMe, AddressOf RemoveMe '用于在绑定后编辑时,取消编辑时从集合中移除.
Me.List.Add(c)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemAdded, List.Count - 1))
Return c
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 绑定后是否允许编辑.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property AllowEdit()Property AllowEdit() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit
Get
Return True
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 绑定后是否允许添加
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property AllowNew()Property AllowNew() As Boolean Implements System.ComponentModel.IBindingList.AllowNew
Get
Return True
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 绑定后是否允许删除
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property AllowRemove()Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove
Get
Return True
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 执行排序.
''' </summary>
''' <param name="property"></param>
''' <param name="direction"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub ApplySort()Sub ApplySort(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort
Me.md_SortDir = direction
Me.Sort = [Property ]()property].DisplayName
End Sub
Public Function Find()Function Find(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor, ByVal key As Object) As Integer Implements System.ComponentModel.IBindingList.Find
End Function
''' -----------------------------------------------------------------------------
''' <summary>
''' 是否正处于排序模式.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property IsSorted()Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted
Get
If Me.md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
Return False
Else
Return True
End If
End Get
End Property
Public Event ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Implements System.ComponentModel.IBindingList.ListChanged
Public Sub RemoveIndex()Sub RemoveIndex(ByVal [Property ]()property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.RemoveIndex
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 取消排序
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Sub RemoveSort()Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort
Me.Sort = String.Empty
End Sub
Public ReadOnly Property SortDirection()Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection
Get
Return Me.md_SortDir
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回正在的排序的列
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property SortProperty()Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty
Get
If Me.md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
Return Nothing
Else
Return New CustomPropertyDescriptor(Me.md_SortName, GetType(Customer).GetProperty(Me.md_SortName))
End If
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 更改通知.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property SupportsChangeNotification()Property SupportsChangeNotification() As Boolean Implements System.ComponentModel.IBindingList.SupportsChangeNotification
Get
Return True
End Get
End Property
Public ReadOnly Property SupportsSearching()Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching
Get
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 是否允许排序.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property SupportsSorting()Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting
Get
Return True
End Get
End Property
#End Region
IComparer#Region "IComparer"
Public Function Compare()Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim objX As Object
Dim objY As Object
Dim ReturnValue As Integer
If TypeOf x Is Customer Then
objX = CType(x, Customer).GetType.GetProperty(Me.md_SortName).GetValue(x, Nothing)
Else
objX = x
End If
If TypeOf y Is Customer Then
objY = CType(y, Customer).GetType.GetProperty(Me.md_SortName).GetValue(y, Nothing)
Else
objY = y
End If
If IsDBNull(objX) Then
If IsDBNull(objY) Then
ReturnValue = 0
Else
ReturnValue = -1
End If
Else
If IsDBNull(objY) Then
ReturnValue = 1
ElseIf objX > objY Then
ReturnValue = 1
ElseIf objX = objY Then
Return 0
ElseIf objX < objY Then
ReturnValue = -1
End If
End If
If Not Me.md_SortDir = ListSortDirection.Ascending Then ReturnValue = -ReturnValue
Return ReturnValue
End Function
#End Region
重写CollectionBase方法#Region "重写CollectionBase方法"
''' -----------------------------------------------------------------------------
''' <summary>
''' 清除集合完成后.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnClearComplete()Sub OnClearComplete()
OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, 0))
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 插入完成后.
''' </summary>
''' <param name="index"></param>
''' <param name="value"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnInsertComplete()Sub OnInsertComplete(ByVal index As Integer, ByVal value As Object)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemAdded, index))
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 移除完成后.
''' </summary>
''' <param name="index"></param>
''' <param name="value"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnRemoveComplete()Sub OnRemoveComplete(ByVal index As Integer, ByVal value As Object)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemDeleted, index))
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 改变集合中项目的值后.
''' </summary>
''' <param name="index"></param>
''' <param name="oldValue"></param>
''' <param name="newValue"></param>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-09-09 Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Overrides Sub OnSetComplete()Sub OnSetComplete(ByVal index As Integer, ByVal oldValue As Object, ByVal newValue As Object)
OnListChanged(New ListChangedEventArgs(ListChangedType.ItemChanged, index))
End Sub
#End Region
Public Function Add()Function Add(ByVal value As Customer) As Integer
Return Me.List.Add(value)
End Function
Public Sub Remove()Sub Remove(ByVal Value As Customer)
Me.List.Remove(Value)
End Sub
Public Property Item()Property Item(ByVal index As Integer) As Customer
Get
Return Me.List.Item(index)
End Get
Set(ByVal Value As Customer)
Me.List.Item(index) = Value
End Set
End Property
End Class
这样我们就可以完成了实体集合的绑定,源码下载:
https://files.cnblogs.com/zqonline/实体和实体集合的绑定.rar
放在首页上献丑了,如果有什么问题,请指教.