Groups | Blog | Home
all groups > dotnet windows forms databinding > october 2004 >

dotnet windows forms databinding : Bind Custom Class to windows form datagrid.


martalex NO[at]SPAM hotmail.com
10/14/2004 8:39:43 PM
I'm trying to bind to a datagrid a custom class with complex property
(custom class). The top data is binding fine but I cannot access the
sub-class property.

Here are my classes... I can bind the contact.id, contact.lastname,
contact.firstname without any problems however I cannot seems to be
able to bind the contact.organization.id and contact.organization.name
to the datagrid. Any ideas how?

.....

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objContactCollection As ContactCollection = New ContactCollection

Dim objContact As Contact

objContact = New Contact
objContact.id = 1
objContact.FirstName = "John"
objContact.LastName = "Smith"
objContact.Organization.id = 10
objContact.Organization.Name = "ABC Corp."
objContactCollection.Add(objContact)

objContact = New Contact
objContact.id = 2
objContact.FirstName = "Tom"
objContact.LastName = "Picard"
objContact.Organization.id = 11
objContact.Organization.Name = "ACME Corp."
objContactCollection.Add(objContact)

objContact = New Contact
objContact.id = 3
objContact.FirstName = "Rick"
objContact.LastName = "Dupont"
objContact.Organization.id = 12
objContact.Organization.Name = "ACME Corp."
objContactCollection.Add(objContact)

DataGrid1.DataSource = objContactCollection


End Sub

.....

Public Class Contact
Private moOrganization As Organization = New Organization
Private mstrLastName As String
Private mstrFirstName As String
Private mintid As Integer

Public Property id() As Integer
Get
Return mintid
End Get
Set(ByVal Value As Integer)
mintid = Value
End Set
End Property
Public Property FirstName() As String
Get
Return mstrFirstName
End Get
Set(ByVal Value As String)
mstrFirstName = Value
End Set
End Property
Public Property LastName() As String
Get
Return mstrLastName
End Get
Set(ByVal Value As String)
mstrLastName = Value
End Set
End Property
Public Property Organization() As Organization
Get
Return moOrganization
End Get
Set(ByVal Value As Organization)
moOrganization = Value
End Set
End Property

Public Sub New()

End Sub

End Class

Public Class ContactCollection
Inherits CollectionBase

Default Public Property Item(ByVal index As Integer) As Contact
Get
Return CType(List(index), Contact)
End Get
Set(ByVal Value As Contact)
List(index) = Value
End Set
End Property

Public Function FindByID(ByVal strID As String)
Dim index As Integer = 0
Dim item As Contact
For Each item In Me
If item.id = strID Then
Return index
End If
index = (index + 1)
Next
Return -1
End Function

Public Function Add(ByVal value As Contact) As Integer
Return (List.Add(value))
End Function

Public Function IndexOf(ByVal value As Contact) As Integer
Return (List.IndexOf(value))
End Function

Public Sub Insert(ByVal index As Integer, ByVal value As Contact)
List.Insert(index, value)
End Sub

Public Sub Remove(ByVal value As Contact)
List.Remove(value)
End Sub

Public Function Contains(ByVal value As Contact) As Boolean
' If value is not of type User, this will return false.
Return (List.Contains(value))
End Function

End Class


Public Class Organization
Private mstrName As String
Private mintid As Integer

Public Property id() As Integer
Get
Return mintid
End Get
Set(ByVal Value As Integer)
mintid = Value
End Set
End Property
Public Property Name() As String
Get
Return mstrName
End Get
Set(ByVal Value As String)
mstrName = Value
End Set
End Property
Sijin Joseph
10/15/2004 9:45:53 AM
This is currently not supported with datagrid as it only scans
properties one level deep. One workaround is to implement a
ICustomTypeDescriptor that will return your nested properties as
top-level properties.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

[quoted text, click to view]
Kenn Scribner
10/15/2004 9:54:49 AM
Interesting...I happen to be trying to do the same thing. I implemented
ICustomTypeDescriptor for my collection (which would return property
descriptors for the items in the collection, when asked), but methods in the
interface were never called (placed breaks). So the DataGrid still only
displays properties in the base class...the derived class properties still
aren't discovered.

Precisely where would one implement ICustomTypeDescriptor if not on the
custom collection? Implementing it in the base class of the bound object
won't (shouldn't) work because the base class has no knowledge of the
derived properties. Implementing it in the derived class won't work because
the DataGrid isn't accessing the derived class. So I'm missing something
here...

Odd that DataGrid isn't accessing derived class properties when the type
given over from the collection is the derived type. I can see not caring
about derived types if the DataGrid were bound to a base class...but not
when bound to a derived class, one (me, anyway) would expect the derived
type's properties to be accessible.

I see, Sijin, that you post a lot of responses in this newsgroup. Thanks for
your efforts. :)
-Kenn

[quoted text, click to view]

Sijin Joseph
10/18/2004 9:26:34 AM
Thanks Kenn, it's a great learning experience for me too. :)

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

[quoted text, click to view]
AddThis Social Bookmark Button