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.
Imports System.ComponentModel
Public Class frmGenericBinding
#Region " Modular Variables "
Private moEmployeeBindingList As BindingList(Of Employee)
#End Region
#Region " Constructors "
Public Sub New()
InitializeComponent()
moEmployeeBindingList = New BindingList(Of Employee)
End Sub
#End Region
#Region " Private Methods "
Private Sub PopulateBindingList()
Dim oEmployee As Employee
For nCnt As Integer = 1 To 50
oEmployee = New Employee
oEmployee.FirstName = "Employee_" & nCnt.ToString
oEmployee.LastName = "Last_" & nCnt.ToString
oEmployee.MiddleInitial = "A"
oEmployee.Address1 = "12" & nCnt.ToString() & " Happy Street"
oEmployee.City = "Charlotte"
oEmployee.State = "NC"
oEmployee.ZipCode = "28211"
oEmployee.Manager = Guid.NewGuid
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()
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.