News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Example code to Bind a collection of objects to a DataGrid

Started by nandagopal, Nov 29, 2008, 06:32 PM

Previous topic - Next topic

nandagopal

The Employee class provides basic FirstName and LastName properties, and a constructor to initialize an object.


Public Class Employee
    Private m_FirstName As String
    Private m_LastName As String
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(ByVal Value As String)
            m_FirstName = Value
        End Set
    End Property
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(ByVal Value As String)
            m_LastName = Value
        End Set
    End Property
    Public Sub New(ByVal first_name As String, ByVal _
        last_name As String)
        m_FirstName = first_name
        m_LastName = last_name
    End Sub
End Class


The program creates a collection of Employee objects. It then simply sets the DataGrid control's DataSource property to the collection. The DataGrid automatically displays the Employee objects' properties and lets the user edit their values.


Dim m_Employees As Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    m_Employees = New Collection
    m_Employees.Add(New Employee("Al", "Archer"))
    m_Employees.Add(New Employee("Bo", "Bench"))
    m_Employees.Add(New Employee("Cy", "Cinder"))
    m_Employees.Add(New Employee("Di", "Diver"))

    DataGrid1.DataSource = m_Employees
End Sub