如何对实体对象进行绑定.

初期.
定义了一个实体的强类型集合.
继承于CollectionBase
由于CollectionBase已经实现了IList接口,我的实体也可以绑定到了datagrid.
默认只要是类的公共属性都会出现在datagrid中.这不是我想要的.翻了一下msdn和在网上查了一下资料,需要使用接口ItypeList才能实现我的要求.

ITypedList接口介绍
提供发现可绑定列表架构的功能,其中可用于绑定的属性不同于要绑定到的对象的公共属性。例如,当使用表示客户表的 DataView 时,您要绑定到 DataView 表示的客户对象上的属性,而不是 DataView 的属性。
接口方法:
GetItemProperties
GetListName
其中最重要的是GetItemProperties 方法,实现它就能自由灵活的控制,要绑定的属性列表.

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

其中方法:GetCustomProperties是用于实体类的那些属性是允许绑定的一个数组.

ContractedBlock.gif ExpandedBlockStart.gif
None.gif        ''' -----------------------------------------------------------------------------
None.gif
        ''' <summary>
None.gif
        ''' 返回进行绑定的属性列表.
None.gif
        ''' </summary>
None.gif
        ''' <returns></returns>
None.gif
        ''' <remarks>
None.gif
        ''' </remarks>
None.gif
        ''' <history>
None.gif
        '''     [zqonline]    2006-09-02    Created
None.gif
        ''' </history>
None.gif
        ''' -----------------------------------------------------------------------------
ExpandedBlockStart.gifContractedBlock.gif
        Public Overridable Function GetCustomProperties()Function GetCustomProperties() As CustomPropertyDescriptor()
InBlock.gif            
Dim cp(Me.BindAttributeList.Count - 1As CustomPropertyDescriptor
InBlock.gif            
Dim i As Integer
InBlock.gif            
For Each str As String In Me.BindAttributeList
InBlock.gif                cp(i) 
= (New CustomPropertyDescriptor(strMe.GetType.GetProperty(str)))
InBlock.gif                i 
+= 1
InBlock.gif            
Next
InBlock.gif
InBlock.gif            
Return cp
ExpandedBlockEnd.gif        
End Function

同时我们还需要实现描述属性的类CustomPropertyDescriptor继承于System.ComponentModel.PropertyDescriptor

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif Public Class CustomPropertyDescriptorClass 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
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(FieldAttribute), 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
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            
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

这样就算定义完成了,可能实现在实体的绑定.
如果还需要在datagrid中进行编辑,添加,移除,排序等操作.还要在实体集合上实现IBindingList接口.

下面是我的持久层实现在对实体集合进行编辑的代码:

获取实体集合

ContractedBlock.gif ExpandedBlockStart.gif
None.gif     '实例化实体
None.gif
        Dim t As New t
None.gif
None.gif        
'实体操作类
None.gif
        Dim ea As New Lily.PersistenceLayer.PersistenceLayerAdapter
None.gif        
'实体查询对象
None.gif
        Dim sc As New Lily.PersistenceLayer.SelectCommand
None.gif        
'只取前面50条
None.gif
        sc.Top = 500
None.gif        
'添加要查询数据的表
None.gif
        sc.FromSoure.AddFrom(t)
None.gif        
'查询对象添加到操作适配器中
None.gif
        ea.AddEntityCommand(sc)
None.gif        
'获取结果
None.gif
        Dim ec As Lily.PersistenceLayer.EntityContainer = ea.GetEntityContainer(GetType(t))
None.gif        ec.ItemType 
= GetType(t)
None.gif        
'把实体集合绑定到datagrid
None.gif
        Me.DataGrid1.DataSource = ec


在datagrid中编辑之后,保存实体

ContractedBlock.gif ExpandedBlockStart.gif
None.gif '持久层操作对象
None.gif
        Dim da As New Lily.PersistenceLayer.PersistenceLayerAdapter
None.gif        
Try
None.gif            da.DataHelper.BeginTransaction() 
'开始事务
None.gif
            '先删除,如果没有指定条件对象,则把实体对应所有的数据都删除
None.gif
            da.AddEntityCommand(New Lily.PersistenceLayer.DeleteCommand(New t, Nothing))
None.gif
None.gif            
'获取对象集合
None.gif
            Dim ec As Lily.PersistenceLayer.EntityContainer = Me.DataGrid1.DataSource
None.gif            
'编历把插入命令对象加入到操作对象中
None.gif
            For Each en As t In ec
None.gif                da.AddEntityCommand(
New Lily.PersistenceLayer.InsertCommand(en))
None.gif            
Next
None.gif
None.gif            
'执行操作
None.gif
            '这里才开始把之前进行更新
None.gif
            da.Update()
None.gif
None.gif            da.DataHelper.Commit()           
'提交事务
None.gif
        Catch ex As Exception
None.gif            da.DataHelper.Rollback()
None.gif            
MsgBox(ex.ToString)
None.gif        
End Try

 

还没有处理的问题
1.在进行实体的插入后,把自动增长字段的值更新到实体
2.实体的属性发生改变后,绑定的控件也能实时发应,.net2.0到有一个接口INotifyPropertyChanged可以实现,不知道.net1.1有没有这样的接口,知道的朋友请踢教.

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

在C#中,可以通过`DataGridView`控件的自动绑定机制,将其列和实体对象的属性进行绑定。只需要将实体对象的集合作为`DataGridView`控件的`DataSource`,并设置`AutoGenerateColumns`属性为`true`,`DataGridView`控件就会自动创建列并绑定实体对象的属性。 以下是示例代码: ```csharp // 假设已经创建了DataGridView控件实例 dgv,并有一个名为 Person 的实体类 // 创建实体对象的集合 List<Person> persons = new List<Person>(); persons.Add(new Person() { Name = "Tom", Age = 20 }); persons.Add(new Person() { Name = "Jerry", Age = 18 }); persons.Add(new Person() { Name = "Alice", Age = 22 }); // 将实体对象的集合作为DataGridView控件的DataSource,并设置AutoGenerateColumns属性为true dgv.DataSource = persons; dgv.AutoGenerateColumns = true; ``` 在上述代码中,首先创建了一个名为`Person`的实体类,包含了`Name`和`Age`两个属性。然后,创建了一个实体对象的集合`persons`,并将其作为`DataGridView`控件的`DataSource`,并设置`AutoGenerateColumns`属性为`true`,这样`DataGridView`控件就会自动创建列并绑定实体对象的属性。 需要注意的是,在使用自动绑定机制时,需要确保实体对象的属性名和`DataGridView`控件中列的名称一致,否则无法正确地绑定数据。如果需要自定义列的显示名称,可以使用`DisplayName`特性来设置。 示例代码如下: ```csharp public class Person { [DisplayName("姓名")] public string Name { get; set; } [DisplayName("年龄")] public int Age { get; set; } } ``` 在上述代码中,使用`DisplayName`特性为`Name`和`Age`属性设置了自定义的显示名称。在`DataGridView`控件中,会将实体对象的属性名替换为`DisplayName`特性中的值,作为列的显示名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值