Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / October 2004

Tip: Looking for answers? Try searching our database.

Bind Custom Class to windows form datagrid.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex - 15 Oct 2004 04:39 GMT
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
End Class
Sijin Joseph - 15 Oct 2004 05:15 GMT
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

> 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
[quoted text clipped - 157 lines]
>     End Property
> End Class
Kenn Scribner - 15 Oct 2004 15:54 GMT
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

> This is currently not supported with datagrid as it only scans
> properties one level deep. One workaround is to implement a
[quoted text clipped - 166 lines]
> > End Property
> > End Class
Sijin Joseph - 18 Oct 2004 04:56 GMT
Thanks Kenn, it's a great learning experience for me too. :)

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

> Interesting...I happen to be trying to do the same thing. I implemented
> ICustomTypeDescriptor for my collection (which would return property
[quoted text clipped - 190 lines]
>>>End Property
>>>End Class

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.