Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

BindingList Example

3.44/5 (15 votes)
29 Jan 2008CPOL3 min read 2   4.3K  
An article on how to create business objects.

Introduction

This is a very basic example of how to use the System.ComponentModel.BindingList. This is the first in a series of articles that will walk the newbie developer from simple data binding to creating a parent/child base business object.

Background

The reason I'm writing these articles is to hopefully help other developers to understand how to create business objects. If you have ever looked at some of the free frameworks out there that you can download and use, such as CSLA (http://www.lhotka.net/cslanet/), I'm not smart enough to pull these down and say, yes I understand how that works! So, while my intention is not to recreate any specific framework, or even create a new one, I do hope to show some of the basic functionality that can be used to create a robust parent/child business object.

Using the code

This example is available in both VB.NET and C# for VS 2005. I didn't want to post all the code here, so download the Zip files for a full example. This is a simple form with a DataGridView named dgvEmployees and a Button to call the Populate method.

VB
' This import is needed so we do not need to user
' the Fully Qualified Name (FQN) for BindingList
Imports System.ComponentModel

Public Class frmGenericBinding

#Region " Modular Variables "
    ' Provides generic collection data binding. Notice that we are specifying 
    ' the type of object that is allowed to be added to the BindingList
    Private moEmployeeBindingList As BindingList(Of Employee)
#End Region

#Region " Constructors "
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Instantiate the needed BindingList
        moEmployeeBindingList = New BindingList(Of Employee)

    End Sub
#End Region

#Region " Private Methods "
 ''' <summary>

''' This method will create 50 new employees and add them to the BindList

''' </summary>

Private Sub PopulateBindingList()
    Dim oEmployee As Employee

    ' Create some Employees
    For nCnt As Integer = 1 To 50
        oEmployee = New Employee

        ' Create a employee first/last name using the counter
        oEmployee.FirstName = "Employee_" & nCnt.ToString
        oEmployee.LastName = "Last_" & nCnt.ToString
        oEmployee.MiddleInitial = "A"

        ' Create a employee street address using the counter
        oEmployee.Address1 = "12" & nCnt.ToString() & " Happy Street"
        oEmployee.City = "Charlotte"
        oEmployee.State = "NC"
        oEmployee.ZipCode = "28211"

        ' Create a manager id - Generate some ID
        oEmployee.Manager = Guid.NewGuid

        ' Add new employee to the BindingList
        moEmployeeBindingList.Add(oEmployee)
        
        // This will compile but will throw a System.InvalidCastException
        // when executed
        //moEmployeeBindingList.Add(new object)

        Next
End Sub

#End Region

Private Sub btnLoadEmployees_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnLoadEmployees.Click
    PopulateBindingList()

    ' Bind the BindingList of Employee objects to the DataGridView
    Me.dgvEmployees.DataSource = moEmployeeBindingList

End Sub

End Class

Looking at the code above, we will start with the BindingList object. The line "Private moEmployeeBindingList As BindingList(Of Employee)" declares a BindingList object and states that it will only hold objects of the Employee type. It may not seem like a big deal if you have not done this before. But, what would happen if you allow any type of object into the collection and then you try to bind that collection or iterate through the collection? If you iterate through the collection looking for the FirstName field and one of the items in your collection does not have that field, it would throw an error. So, type safety is a good thing!

The next thing to notice is the "btnLoadEmployees_Click". This method creates 50 new employee objects for us and puts them in the BindingList and the sets the DataGrid's DataSource to the BindingList. To display our collection of Employee objects, all we have to do is create the Employee objects, put them in the BindingList (it's used as our collection object right now as well as for handling the binding), and set the DataSource.

We have our data in the DataGrid, now what? Well, pick any cell in the grid and double click on it so that you can edit the cell's value. Also make sure that you have the "Output" window in view. Now, change any value and tab off the cell or click another cell. Notice that in the Output window, you will see a message that states "Property xx has been changed." When you change a value, the BindingList persists that change back to it's Employee object. Cool as heck if you ask me.

This is the end of the first installment. In the next installment, we will create our own collection object that inherits from BindingList and add some more logic to our Employee object.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)