Click here to Skip to main content
15,562,410 members
Articles / Programming Languages / Visual Basic
Posted 26 Jan 2008


32 bookmarked

BindingList Example

Rate me:
Please Sign up or sign in to vote.
3.44/5 (15 votes)
29 Jan 2008CPOL3 min read
An article on how to create business objects.


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.


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 (, 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.

' 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.
        ' 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
        // This will compile but will throw a System.InvalidCastException
        // when executed
        //moEmployeeBindingList.Add(new object)

End Sub

#End Region

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

    ' 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.


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

Written By
Software Developer (Senior) National Diagnostics
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionwhat if in the binding object you have list ? Pin
Member 1218369414-Dec-15 12:40
Member 1218369414-Dec-15 12:40 
GeneralMy vote of 5 Pin
Raja Sekhar S3-Jun-13 20:13
Raja Sekhar S3-Jun-13 20:13 
QuestionAdvanced BindingList Question Pin
Habib Ayob30-Mar-11 11:57
Habib Ayob30-Mar-11 11:57 
AnswerRe: Advanced BindingList Question Pin
Sean Rhone30-Mar-11 12:35
Sean Rhone30-Mar-11 12:35 
GeneralRe: Advanced BindingList Question Pin
Habib Ayob30-Mar-11 13:00
Habib Ayob30-Mar-11 13:00 
GeneralMy vote of 1 Pin
lchanlaval20-Oct-10 4:28
lchanlaval20-Oct-10 4:28 
QuestionMaybe beginner question Pin
mslliviu23-Jun-10 4:14
mslliviu23-Jun-10 4:14 
AnswerRe: Maybe beginner question Pin
Sean Rhone6-Jul-10 4:36
Sean Rhone6-Jul-10 4:36 
GeneralHi Guys Pin
CatLiz21-Feb-10 6:34
CatLiz21-Feb-10 6:34 
GeneralThanks Pin
jinkazuma1-Sep-09 9:07
jinkazuma1-Sep-09 9:07 
Generalsome videos to the topic Pin
Mr.PoorEnglish30-Jan-08 12:00
Mr.PoorEnglish30-Jan-08 12:00 
GeneralBasic stuff Pin
hshmed30-Jan-08 4:00
hshmed30-Jan-08 4:00 
GeneralRe: Basic stuff PinPopular
victorbos30-Jan-08 4:36
victorbos30-Jan-08 4:36 
Indeed that is his point. It takes a lot of effort for people to write articles to generously share with others. If you don't have anything constructive to say, it is better for all if you don't. On the other hand, if you think you can do better, please DO write a "non-basic" article and we will all appreciate it.
GeneralRe: Basic stuff Pin
Ruben Fuchs21-May-12 4:06
Ruben Fuchs21-May-12 4:06 
Generalgood code Pin
cvb228-Jan-08 16:09
cvb228-Jan-08 16:09 
GeneralThanks Pin
victorbos28-Jan-08 4:51
victorbos28-Jan-08 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.