Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleBind a collection of objects to a DataGrid in VB .NET
DescriptionThis example shows how to bind a collection of objects to a DataGrid in VB .NET.
KeywordsDataGrid, bind, data binding, collection
CategoriesVB.NET, Database, Controls
 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated