I think you need to create public properties for each column you want to
show on the DataGrid.
So try to make _Name and _Dept public proerties and check.
r.

-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Girish Jain
Sent: Monday, November 22, 2004 7:50 PM
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Binding Custom Collections to WinForms DataGrid

Hi All,

I am having problem in binding a custom collection to a DataGrid. I have
created a collection and binding it to the DataGrid but it does not show
data on the grid. The code executes without any errors. I know I am missing
(probably a lot of things!), that's why I seek your help. Here's the code..

Public NotInheritable Class Employee
    Private _Name As String
    Private _Dept As String

    Public Sub New(ByVal Name As String, ByVal Dept As String)
        Me._Name = Name
        Me._Dept = Dept
    End Sub

    Public Overrides Function ToString() As String
        Return String.Format("Employee Name : {0} And Department is {1}",
Me._Name, Me._Dept)
    End Function
End Class


Public NotInheritable Class EmployeeCollection
    Inherits CollectionBase

    Public Function Add(ByVal obj As Employee) As Integer
        Return MyBase.InnerList.Add(obj)
    End Function

    Public Sub Remove(ByVal obj As Employee)
        MyBase.InnerList.Remove(obj)
    End Sub

    Default Public Property Item(ByVal index As Integer) As Employee
        Get
            If index > MyBase.InnerList.Count - 1 OrElse index < 0 Then
                Throw New ArgumentOutOfRangeException("The specified
argument is out of range")
            End If
            Return DirectCast(MyBase.InnerList(index), Employee)
        End Get
        Set(ByVal Value As Employee)
            MyBase.InnerList(index) = Value
        End Set
    End Property
End Class


This is the code I wrote in form

        Dim a As New EmployeeCollection
        a.Add(New Employee("John", "Accounts"))
        a.Add(New Employee("John", "Accounts"))

       dgTest.DataSource = a


Thanks in advance

Regards,
Girish Jain

_________________________________________________________________
The Citi Rupee Checking Acct. Send money home free!
http://creative.mediaturf.net/creatives/citibankrca/rca_msntextlinks.htm Get
fabulous benefits too!

===================================
This list is hosted by DevelopMentorR  http://www.develop.com Some .NET
courses you may be interested in:

Essential .NET: building applications and components with C# November 29 -
December 3, in Los Angeles http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with C#
November 29 - December 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to