实体与实体集合的绑定

开发环境:Vs2003
实体类实现了IEditableObject接口,IEditableObject接口的作用是在绑定后可以按Esc取消先前输入的值.
None.gif ' '' -----------------------------------------------------------------------------
None.gif'
'' Project     : 实体和实体集合的绑定
None.gif'
'' Class     : Customer
None.gif'
'' 
None.gif'
'' -----------------------------------------------------------------------------
None.gif'
'' <summary>
None.gif'
'' 实体类.
None.gif'
'' </summary>
None.gif'
'' <remarks>
None.gif'
'' </remarks>
None.gif'
'' <history>
None.gif'
''     [zqonline]    2006-09-09    Created
None.gif'
'' </history>
None.gif'
'' -----------------------------------------------------------------------------
ExpandedBlockStart.gifContractedBlock.gif
Public   Class Customer Class Customer
InBlock.gif    
Implements System.ComponentModel.IEditableObject
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
成员变量#Region "成员变量"
InBlock.gif    
Private md_id As Integer
InBlock.gif    
Private md_name As String = String.Empty
InBlock.gif    
Private md_age As Integer
InBlock.gif
InBlock.gif    
Private md_isNew As Boolean = True
InBlock.gif    
Private md_isEdit As Boolean = False
InBlock.gif    
Private md_editCustomer As Customer
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
类实例化#Region "类实例化"
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub New()Sub New()
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub New()Sub New(ByVal id As Integer)
InBlock.gif        
Me.New()
InBlock.gif        
Me.ID = id
ExpandedSubBlockEnd.gif    
End Sub

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
事件声明#Region "事件声明"
InBlock.gif    
Public Event RemoveMe(ByVal customer As Customer)
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
实体属性#Region "实体属性"
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property ID()Property ID() As Integer
InBlock.gif        
Get
InBlock.gif            
Return Me.md_id
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Integer)
InBlock.gif            
Me.md_id = Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property Name()Property Name() As String
InBlock.gif        
Get
InBlock.gif            
Return Me.md_name
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            
Me.md_name = Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property Age()Property Age() As String
InBlock.gif        
Get
InBlock.gif            
Return Me.md_age
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            
Me.md_age = Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
IEditableObject#Region "IEditableObject"
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 开始编辑.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub BeginEdit()Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
InBlock.gif        
If Me.md_isNew Then
InBlock.gif        
Else
InBlock.gif            
If Not Me.md_isEdit Then
InBlock.gif
InBlock.gif
InBlock.gif                
'处于编辑的标志
InBlock.gif
                Me.md_isEdit = True
InBlock.gif
InBlock.gif                
Me.md_editCustomer = New Customer
InBlock.gif
InBlock.gif                
'保存起来.便于在执行CancelEdit方法时恢复.
InBlock.gif
                Me.md_editCustomer.Name = Me.Name
InBlock.gif                
Me.md_editCustomer.ID = Me.ID
InBlock.gif                
Me.md_editCustomer.Age = Me.Age
InBlock.gif            
End If
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 取消编辑.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub CancelEdit()Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
InBlock.gif        
If Me.md_isNew Then
InBlock.gif            
RaiseEvent RemoveMe(Me)
InBlock.gif        
Else
InBlock.gif            
'判断是否处于编辑模式
InBlock.gif
            If Me.md_isEdit Then
InBlock.gif                
Me.Age = Me.md_editCustomer.Age
InBlock.gif                
Me.ID = Me.md_editCustomer.ID
InBlock.gif                
Me.Name = Me.md_editCustomer.Name
InBlock.gif
InBlock.gif            
End If
InBlock.gif        
End If
InBlock.gif
InBlock.gif        
Me.md_isEdit = False
InBlock.gif        
Me.md_isNew = False
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 结束编辑.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub EndEdit()Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
InBlock.gif        
Me.md_isNew = False
InBlock.gif        
Me.md_isEdit = False
ExpandedSubBlockEnd.gif    
End Sub

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif

实体集合主要实现了ITypedList接口,主要作用是用于可以自定义可绑定的实体属性.IBindingList接口主要是用于绑定后是否允许编辑,是否允许删除,是否允许添加,排序等丰富的绑定属性定义.IComparer对排序的支持.
ContractedBlock.gif ExpandedBlockStart.gif 实体集合
None.gifImports System.ComponentModel
None.gif
''' -----------------------------------------------------------------------------
None.gif'
'' Project     : 实体和实体集合的绑定
None.gif'
'' Class     : CustomersList
None.gif'
'' 
None.gif'
'' -----------------------------------------------------------------------------
None.gif'
'' <summary>
None.gif'
'' 实体集合.
None.gif'
'' </summary>
None.gif'
'' <remarks>
None.gif'
'' </remarks>
None.gif'
'' <history>
None.gif'
''     [zqonline]    2006-09-09    Created
None.gif'
'' </history>
None.gif'
'' -----------------------------------------------------------------------------
ExpandedBlockStart.gifContractedBlock.gif
Public Class CustomersListClass CustomersList
InBlock.gif    
Inherits CollectionBase
InBlock.gif    
Implements System.ComponentModel.ITypedList, System.ComponentModel.IBindingList, System.Collections.IComparer
InBlock.gif
InBlock.gif    
'排序的列
InBlock.gif
    Private md_SortName As String = String.Empty
InBlock.gif    
'排序方式
InBlock.gif
    Private md_SortDir As System.ComponentModel.ListSortDirection = ListSortDirection.Ascending
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
ITypedList#Region "ITypedList"
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 在实际中,可以控制实体的那些属性可以进行绑定.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <param name="listAccessors"></param>
InBlock.gif
    ''' <returns></returns>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Function GetItemProperties()Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties
InBlock.gif        
Dim pd(2As PropertyDescriptor
InBlock.gif        
Dim c As New Customer
InBlock.gif
InBlock.gif        
'反射是对大小敏感的.
InBlock.gif
        pd(0= New CustomPropertyDescriptor("ID", c.GetType.GetProperty("ID"))
InBlock.gif        pd(
1= New CustomPropertyDescriptor("Age", c.GetType.GetProperty("Age"))
InBlock.gif        pd(
2= New CustomPropertyDescriptor("Name", c.GetType.GetProperty("Name"))
InBlock.gif
InBlock.gif        
Return New System.ComponentModel.PropertyDescriptorCollection(pd)
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Function GetListName()Function GetListName(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As String Implements System.ComponentModel.ITypedList.GetListName
InBlock.gif        
Return ""
ExpandedSubBlockEnd.gif    
End Function

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
IBindingList#Region "IBindingList"
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub RemoveMe()Sub RemoveMe(ByVal c As Customer)
InBlock.gif        
Me.List.Remove(c)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 引发集合项发生改变时的事件.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <param name="e"></param>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overridable Sub OnListChanged()Sub OnListChanged(ByVal e As System.ComponentModel.ListChangedEventArgs)
InBlock.gif        
RaiseEvent ListChanged(Me, e)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Property Sort()Property Sort() As String
InBlock.gif        
Get
InBlock.gif            
Return Me.md_SortName
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            md_SortName 
= Value
InBlock.gif            
If md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
InBlock.gif                
Me.md_SortName = String.Empty
InBlock.gif            
Else
InBlock.gif                
Me.InnerList.Sort(Me)
InBlock.gif
InBlock.gif                
Me.OnListChanged(New System.ComponentModel.ListChangedEventArgs(ListChangedType.Reset, 0))
InBlock.gif            
End If
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub AddIndex()Sub AddIndex(ByVal [Property ]()propertyAs System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.AddIndex
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Function AddNew()Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew
InBlock.gif        
Dim c As New Customer
InBlock.gif        
AddHandler c.RemoveMe, AddressOf RemoveMe   '用于在绑定后编辑时,取消编辑时从集合中移除.
InBlock.gif
        Me.List.Add(c)
InBlock.gif
InBlock.gif        OnListChanged(
New ListChangedEventArgs(ListChangedType.ItemAdded, List.Count - 1))
InBlock.gif
InBlock.gif        
Return c
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 绑定后是否允许编辑.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <value></value>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property AllowEdit()Property AllowEdit() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit
InBlock.gif        
Get
InBlock.gif            
Return True
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 绑定后是否允许添加
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <value></value>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property AllowNew()Property AllowNew() As Boolean Implements System.ComponentModel.IBindingList.AllowNew
InBlock.gif        
Get
InBlock.gif            
Return True
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 绑定后是否允许删除
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <value></value>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property AllowRemove()Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove
InBlock.gif        
Get
InBlock.gif            
Return True
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 执行排序.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <param name="property"></param>
InBlock.gif
    ''' <param name="direction"></param>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub ApplySort()Sub ApplySort(ByVal [Property ]()propertyAs System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort
InBlock.gif        
Me.md_SortDir = direction
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Me.Sort = [Property ]()property].DisplayName
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Function Find()Function Find(ByVal [Property ]()propertyAs System.ComponentModel.PropertyDescriptor, ByVal key As ObjectAs Integer Implements System.ComponentModel.IBindingList.Find
InBlock.gif
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 是否正处于排序模式.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <value></value>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property IsSorted()Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted
InBlock.gif        
Get
InBlock.gif            
If Me.md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
InBlock.gif                
Return False
InBlock.gif            
Else
InBlock.gif                
Return True
InBlock.gif            
End If
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
Public Event ListChanged(ByVal sender As ObjectByVal e As System.ComponentModel.ListChangedEventArgs) Implements System.ComponentModel.IBindingList.ListChanged
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub RemoveIndex()Sub RemoveIndex(ByVal [Property ]()propertyAs System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.RemoveIndex
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 取消排序
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub RemoveSort()Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort
InBlock.gif        
Me.Sort = String.Empty
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property SortDirection()Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection
InBlock.gif        
Get
InBlock.gif            
Return Me.md_SortDir
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 返回正在的排序的列
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <value></value>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property SortProperty()Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty
InBlock.gif        
Get
InBlock.gif            
If Me.md_SortName Is Nothing OrElse Me.md_SortName.Length = 0 Then
InBlock.gif                
Return Nothing
InBlock.gif            
Else
InBlock.gif                
Return New CustomPropertyDescriptor(Me.md_SortName, GetType(Customer).GetProperty(Me.md_SortName))
InBlock.gif            
End If
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 更改通知.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <value></value>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property SupportsChangeNotification()Property SupportsChangeNotification() As Boolean Implements System.ComponentModel.IBindingList.SupportsChangeNotification
InBlock.gif        
Get
InBlock.gif            
Return True
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property SupportsSearching()Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching
InBlock.gif        
Get
InBlock.gif
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 是否允许排序.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <value></value>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property SupportsSorting()Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting
InBlock.gif        
Get
InBlock.gif            
Return True
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
IComparer#Region "IComparer"
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Function Compare()Function Compare(ByVal x As ObjectByVal y As ObjectAs Integer Implements System.Collections.IComparer.Compare
InBlock.gif        
Dim objX As Object
InBlock.gif        
Dim objY As Object
InBlock.gif
InBlock.gif        
Dim ReturnValue As Integer
InBlock.gif
InBlock.gif        
If TypeOf x Is Customer Then
InBlock.gif            objX 
= CType(x, Customer).GetType.GetProperty(Me.md_SortName).GetValue(x, Nothing)
InBlock.gif        
Else
InBlock.gif            objX 
= x
InBlock.gif        
End If
InBlock.gif
InBlock.gif        
If TypeOf y Is Customer Then
InBlock.gif            objY 
= CType(y, Customer).GetType.GetProperty(Me.md_SortName).GetValue(y, Nothing)
InBlock.gif        
Else
InBlock.gif            objY 
= y
InBlock.gif        
End If
InBlock.gif
InBlock.gif        
If IsDBNull(objX) Then
InBlock.gif            
If IsDBNull(objY) Then
InBlock.gif                ReturnValue 
= 0
InBlock.gif            
Else
InBlock.gif                ReturnValue 
= -1
InBlock.gif            
End If
InBlock.gif        
Else
InBlock.gif            
If IsDBNull(objY) Then
InBlock.gif                ReturnValue 
= 1
InBlock.gif            
ElseIf objX > objY Then
InBlock.gif                ReturnValue 
= 1
InBlock.gif            
ElseIf objX = objY Then
InBlock.gif                
Return 0
InBlock.gif            
ElseIf objX < objY Then
InBlock.gif                ReturnValue 
= -1
InBlock.gif            
End If
InBlock.gif        
End If
InBlock.gif
InBlock.gif        
If Not Me.md_SortDir = ListSortDirection.Ascending Then ReturnValue = -ReturnValue
InBlock.gif        
Return ReturnValue
InBlock.gif
ExpandedSubBlockEnd.gif    
End Function

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
重写CollectionBase方法#Region "重写CollectionBase方法"
InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 清除集合完成后.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnClearComplete()Sub OnClearComplete()
InBlock.gif        OnListChanged(
New ListChangedEventArgs(ListChangedType.Reset, 0))
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 插入完成后.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <param name="index"></param>
InBlock.gif
    ''' <param name="value"></param>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnInsertComplete()Sub OnInsertComplete(ByVal index As IntegerByVal value As Object)
InBlock.gif        OnListChanged(
New ListChangedEventArgs(ListChangedType.ItemAdded, index))
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 移除完成后.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <param name="index"></param>
InBlock.gif
    ''' <param name="value"></param>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnRemoveComplete()Sub OnRemoveComplete(ByVal index As IntegerByVal value As Object)
InBlock.gif        OnListChanged(
New ListChangedEventArgs(ListChangedType.ItemDeleted, index))
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif    
''' -----------------------------------------------------------------------------
InBlock.gif
    ''' <summary>
InBlock.gif
    ''' 改变集合中项目的值后.
InBlock.gif
    ''' </summary>
InBlock.gif
    ''' <param name="index"></param>
InBlock.gif
    ''' <param name="oldValue"></param>
InBlock.gif
    ''' <param name="newValue"></param>
InBlock.gif
    ''' <remarks>
InBlock.gif
    ''' </remarks>
InBlock.gif
    ''' <history>
InBlock.gif
    '''     [zqonline]    2006-09-09    Created
InBlock.gif
    ''' </history>
InBlock.gif
    ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overrides Sub OnSetComplete()Sub OnSetComplete(ByVal index As IntegerByVal oldValue As ObjectByVal newValue As Object)
InBlock.gif        OnListChanged(
New ListChangedEventArgs(ListChangedType.ItemChanged, index))
ExpandedSubBlockEnd.gif    
End Sub

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Function Add()Function Add(ByVal value As Customer) As Integer
InBlock.gif        
Return Me.List.Add(value)
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub Remove()Sub Remove(ByVal Value As Customer)
InBlock.gif        
Me.List.Remove(Value)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property Item()Property Item(ByVal index As IntegerAs Customer
InBlock.gif        
Get
InBlock.gif            
Return Me.List.Item(index)
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal Value As Customer)
InBlock.gif            
Me.List.Item(index) = Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedBlockEnd.gif
End Class

None.gif


这样我们就可以完成了实体集合的绑定,源码下载:
http://files.cnblogs.com/zqonline/实体和实体集合的绑定.rar

放在首页上献丑了,如果有什么问题,请指教.

转载于:https://www.cnblogs.com/zqonline/archive/2006/09/09/499800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值