Complex Databinding in a DataGridView (WinForms)

Posts   
 
    
bchebrou
User
Posts: 18
Joined: 14-Feb-2006
# Posted on: 16-Feb-2007 16:42:33   

Hello,

I've a little problem with databinding an EntityCollection(of Entity) in a DataGrid.

Let's say i've one entity IncidentEntity and this entity has one property of type AddressEntity (1-1 relation).

In the DataGridView (simple .Net DataGridView), I want to show some properties of the incident and in one column show the address information.

Address information are defined like this : I overrided the ToString() method of the AddressEntity to concat some properties (Street Code + Street + Town + City).

Well, Now if i want to show address information I can create a new property on the IncidentEntity

    Public ReadOnly Property AddressInfo() As String
        Get
            If Me.Address IsNot Nothing Then
                Return Me.Address.ToString()
            Else
                Return Nothing
            End If
        End Get
    End Property

But, i don't want of this rage

So, I read Frans' article on Complex Databinding and ITypedList. I decided to use PropertyDescriptor and ITypedList.GetItemProperties().

But i'm not sure of using it in the best way... flushed

I call GetItemProperties() on the DefaultView of the EntityCollection(of IncidentEntity). After I search in the propertyCollection the propertyDescriptor corresponding to the AddressProperty of the IncidentEntity. I remove it, and add a new AddressPropertyDescriptor to the collection.

Public Class AddressPropertyDescriptor
    Inherits PropertyDescriptor

    Public Overrides Function GetValue(ByVal component As Object) As Object

        Dim obj As IncidentEntity = CType(component, IncidentEntity)

        If obj.AddressOrderedSpNo IsNot Nothing Then
            Return obj.AddressOrderedSpNo.ToString()
        Else
            Return String.Empty
        End If

    End Function

End Class

I'm not very happy of this design... do you see a better way to achieve this ?

Cheers,

Ben

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 17-Feb-2007 12:01:13   

I think the custom property you wrote in the Incident entity is the best approach for this. It's the most natural one. What exactly is it that you don't like about this approach?

Frans Bouma | Lead developer LLBLGen Pro