我来说说自定义对象集合的绑定

我只是在这里抛砖引玉,这其中好多结果都是参照其他技术人员的一些例子和介绍,不过我始终没有找到全面的介绍(就是事无具细的)不过这样也好,所以只能自己摸索,这其中我也有好多不解之处.

我就按我如何来解决这些问题来写写.

我的自定义对象集合是是继承于CollectionBase的.因为CollectionBase已经实现了ILIST接口,也就是说可能直接绑定到了DataGrid控件.但是在默认情况自定义类的公共属性都会绑定到datagrid.这显然是不符合要求的.
按我们实际使用情况如下:
1.可以指定要绑定的属性.
2.可以控制绑定属性的列位置.
这时通过查msdn发现了接口ItypedList好像可以实现这些要求.通过google也找到一个关于绑定的例子.如何实现itypedlist接口.最重要的接口方法是GetItemProperties,他就能控制你自定义的那些属性可以进行绑定.返回的值是PropertyDescriptorCollection集合,所以我们需要实现一个自定义属性描述的类CustomPropertyDescriptor它的基类是System.ComponentModel.PropertyDescriptor.重写其抽象方法.其中要注意的是IsReadOnly属性,这个属性就可以控制在datagrid控件中是否允许编辑此属性.
CustomPropertyDescriptor的实现

ExpandedBlockStart.gif ContractedBlock.gif   Public   Class CustomPropertyDescriptor Class CustomPropertyDescriptor
InBlock.gif        
Inherits System.ComponentModel.PropertyDescriptor
InBlock.gif
InBlock.gif
InBlock.gif        
Private md_name As String = String.Empty
InBlock.gif        
Private md_pi As System.Reflection.PropertyInfo
InBlock.gif        
Private md_attribute As FieldAttribute
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Sub New()Sub New(ByVal name As StringByVal pi As System.Reflection.PropertyInfo)
InBlock.gif         
InBlock.gif            
MyBase.New(name, pi.GetCustomAttributes(GetType(Attribute), True))
InBlock.gif            
Me.md_name = name
InBlock.gif            
Me.md_pi = pi
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides Function GetValue()Function GetValue(ByVal component As ObjectAs Object
InBlock.gif            
'Debug.WriteLine("GetValue")
InBlock.gif
            Return CType(component, EntityBase).GetValue(Me.md_pi.Name)
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides ReadOnly Property ComponentType()Property ComponentType() As System.Type
InBlock.gif            
Get
InBlock.gif                
Return Me.md_pi.DeclaringType
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides ReadOnly Property PropertyType()Property PropertyType() As System.Type
InBlock.gif            
Get
InBlock.gif                
Return Me.md_pi.PropertyType
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides Sub ResetValue()Sub ResetValue(ByVal component As Object)
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides Sub SetValue()Sub SetValue(ByVal component As ObjectByVal value As Object)
InBlock.gif            
'Debug.WriteLine("SetValue")
InBlock.gif
            CType(component, EntityBase).SetValue(Me.md_pi.Name, value)
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides Function CanResetValue()Function CanResetValue(ByVal component As ObjectAs Boolean
InBlock.gif            
Return False
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides Function ShouldSerializeValue()Function ShouldSerializeValue(ByVal component As ObjectAs Boolean
InBlock.gif            
Return False
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Overrides ReadOnly Property IsReadOnly()Property IsReadOnly() As Boolean
InBlock.gif            
Get
InBlock.gif                
Return Not Me.md_pi.CanWrite
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
ExpandedBlockEnd.gif    
End Class

以下强类型集合的实现
    Public Class EntityContainer
        Inherits CollectionBase
        Implements ITypedList, IBindingList
        

ContractedBlock.gif ExpandedBlockStart.gif ItypeList接口 #Region "ItypeList接口"
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
InBlock.gif            
If Me.ItemType Is Nothing Then
InBlock.gif                
Throw New PersistenceLayerException("执行绑定失败,没有指定容器的属性ItemType.")
InBlock.gif            
Else
InBlock.gif                
Dim en As EntityBase = Activator.CreateInstance(Me.ItemType)
InBlock.gif                
Return New System.ComponentModel.PropertyDescriptorCollection(en.GetCustomProperties)
InBlock.gif            
End If
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            
If Me.ItemType Is Nothing Then
InBlock.gif                
Throw New PersistenceLayerException("执行绑定失败,没有指定容器的属性ItemType.")
InBlock.gif            
Else
InBlock.gif                
Return Me.ItemType.Name
InBlock.gif            
End If
ExpandedSubBlockEnd.gif        
End Function

ExpandedBlockEnd.gif
#End Region

以前就是我的ItypedList接口的实现其中属性ItemType是指定我当前集合中存放的实体类型.我已定义的实体具有方法GetCustomProperties就是用于返回我当前实体那些属性是允许进行绑定的.

ContractedBlock.gif ExpandedBlockStart.gif IBindingList接口实现 #Region "IBindingList接口实现"
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
公共#Region "公共"
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 将新项添加到列表。
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <returns></returns>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Function AddNew()Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew
InBlock.gif            
If Not AllowNew Then Throw New InvalidOperationException("AddNew is not allowed.")
InBlock.gif            
Dim en As EntityBase = Activator.CreateInstance(Me.ItemType)
InBlock.gif            List.Add(en)
InBlock.gif
InBlock.gif            
'集合发生改变
InBlock.gif
            OnListChanged(New ListChangedEventArgs(ListChangedType.ItemAdded, List.Count - 1))
InBlock.gif
InBlock.gif            
'注册事件
InBlock.gif
            AddHandler en.RemoveMe, AddressOf Me.RemoveMe
InBlock.gif
InBlock.gif            
Return en
ExpandedSubBlockEnd.gif        
End Function

InBlock.gif
InBlock.gif        
Public Event ListChanged(ByVal sender As ObjectByVal e As System.ComponentModel.ListChangedEventArgs) Implements System.ComponentModel.IBindingList.ListChanged
InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 获取当列表更改或列表中的项更改时是否引发 ListChanged 事件.默认是True
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <value></value>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    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
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
私有#Region "私有"
InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 将 PropertyDescriptor 添加到用于搜索的索引。
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="property"></param>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private 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        
Private ReadOnly Property AllowEdit1()Property AllowEdit1() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit
InBlock.gif            
Get
InBlock.gif                
Return Me.AllowEdit
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Private ReadOnly Property AllowNew1()Property AllowNew1() As Boolean Implements System.ComponentModel.IBindingList.AllowNew
InBlock.gif            
Get
InBlock.gif                
Return Me.AllowNew
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Private ReadOnly Property AllowRemove()Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove
InBlock.gif            
Get
InBlock.gif                
Return Me.AllowDelete
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 根据 PropertyDescriptor 和 ListSortDirection 对列表进行排序。
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-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private Sub ApplySort()Sub ApplySort(ByVal [Property ]()propertyAs System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 返回具有给定 PropertyDescriptor 的行的索引。
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="property"></param>
InBlock.gif
        ''' <param name="key"></param>
InBlock.gif
        ''' <returns></returns>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private 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-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private ReadOnly Property IsSorted()Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted
InBlock.gif            
Get
InBlock.gif
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 从用于搜索的索引中移除 PropertyDescriptor。
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <param name="property"></param>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private 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
        ''' <value></value>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private ReadOnly Property SortDirection()Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection
InBlock.gif            
Get
InBlock.gif
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-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private ReadOnly Property SupportsSorting()Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting
InBlock.gif            
Get
InBlock.gif                
Return False
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 获取列表是否支持使用 Find 方法进行搜索。默认是False
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <value></value>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private ReadOnly Property SupportsSearching()Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching
InBlock.gif            
Get
InBlock.gif                
Return False
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 使用 ApplySort 移除任何已应用的排序。
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private Sub RemoveSort()Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort
InBlock.gif
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
InBlock.gif        
''' -----------------------------------------------------------------------------
InBlock.gif
        ''' <summary>
InBlock.gif
        ''' 获取正在用于排序的 PropertyDescriptor。
InBlock.gif
        ''' </summary>
InBlock.gif
        ''' <value></value>
InBlock.gif
        ''' <remarks>
InBlock.gif
        ''' </remarks>
InBlock.gif
        ''' <history>
InBlock.gif
        '''     [zqonline]    2006-09-02    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Private ReadOnly Property SortProperty()Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty
InBlock.gif            
Get
InBlock.gif
InBlock.gif            
End Get
ExpandedSubBlockEnd.gif        
End Property

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedBlockEnd.gif
#End Region

实现了ItypedList接口,虽然我们绑定到datagrid已实现了我们的要求,但是这时,不能编辑,不能添加,不能删除,如果要实现这些功能,我们就需要实现IBindingList接口.这个接口主要是用于是否允许编辑,是否允许移除,排序,查找等,排序和查找我没有实现.

这时我以为现在可以正常的完成了绑定,不过事情还没有完,我在进行编辑的时候,有时会有异常产生,在通过google查询绑定方面的资料时,对集合的项目进行改变时要引ListChanged事件.
所以需要重写CollectionBase的一些方法

None.gif
ContractedBlock.gifExpandedBlockStart.gif
重写CollectionBase的方法 #Region "重写CollectionBase的方法"
InBlock.gif        
'在向 CollectionBase 实例中插入新元素之后执行其他自定义进程。
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        
'在从 CollectionBase 实例中移除元素之后执行其他自定义进程。
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        
'在清除 CollectionBase 实例的内容之后执行其他自定义进程。
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Protected Overrides Sub OnClearComplete()Sub OnClearComplete()
InBlock.gif            OnListChanged(
New ListChangedEventArgs(ListChangedType.Reset, 0))
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif        
'当在 CollectionBase 实例中设置值后执行其他自定义进程。
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

ExpandedBlockEnd.gif
#End Region

这样这个实体的绑定基本就完成了,不过我在使用过程出现了一些问题,第一个是我不能像datatabel绑定datagrid那样按esc取消编辑,同时存在一些情况,我不能往datagird里输入值,虽然行是添加成功了,百思不得其解.又查资料.如果要实现按esc取消编辑的功能需要对自定义类实现接口IEditableObject.以下是我实现接口的代码


ContractedBlock.gif ExpandedBlockStart.gif IEditableObject #Region " IEditableObject "
InBlock.gif
InBlock.gif        
'编辑标志
InBlock.gif
        Private md_Editing As Boolean
InBlock.gif        
'存放当前正在编辑的实例
InBlock.gif
        Private md_editEntity As EntityBase
InBlock.gif        
'当进行绑时,并取消编辑时,移除对象
InBlock.gif
        Private md_IsNew As Boolean = True
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-03    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                
'绑定添加
InBlock.gif
            Else
InBlock.gif                
'绑定编辑
InBlock.gif
                If Not md_Editing Then
InBlock.gif                    
Me.md_Editing = True
InBlock.gif                    
Me.md_editEntity = Activator.CreateInstance(Me.GetType)
InBlock.gif
InBlock.gif                    
For Each str As String In Me.AttributeList
InBlock.gif                        
Me.md_editEntity.SetValue(strMe.GetValue(str))
InBlock.gif                    
Next
InBlock.gif                
End If
InBlock.gif            
End If
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-03    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Sub CancelEdit()Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
InBlock.gif
InBlock.gif            
'在绑定时移除新增的对象
InBlock.gif
            If Me.md_IsNew Then
InBlock.gif                
RaiseEvent RemoveMe(Me)
InBlock.gif            
Else
InBlock.gif                
For Each str As String In Me.AttributeList
InBlock.gif                    
Me.SetValue(strMe.md_editEntity.GetValue(str))
InBlock.gif                
Next
InBlock.gif            
End If
InBlock.gif
InBlock.gif            
Me.md_Editing = False
InBlock.gif            
Me.md_IsNew = False
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-03    Created
InBlock.gif
        ''' </history>
InBlock.gif
        ''' -----------------------------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Public Sub EndEdit()Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
InBlock.gif            
Me.md_Editing = False
InBlock.gif            
Me.md_IsNew = False
ExpandedSubBlockEnd.gif        
End Sub

InBlock.gif
ExpandedBlockEnd.gif
#End Region

None.gif

注:我的getValue和setValue方法,是我实体自定义的方法.
这时我才把我自定义实体集合进行绑定时,就没有发现问题了.当然还可以实现IDataErrorInfo 接口.这样当用户在操作datagrid时,输入不合法,就会实时给出提示.
同时我也在我的自定义实体集合增加了rest方法.主要是用于通知绑定控件需要进行数据刷新.

        '略去部份代码 
   End Class
   

完成....

参考文章:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet02252003.asp

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值