Ever wonder about the magic behind binding objects to data sources? For example, you can create a strongly typed dataset or build your own datatable object, then fill it with custom data and just bind the data to a gridview control. Well... we make our own bindable objects too in VS 2005
Start by Importing one of my favorite namespaces ComponentModel
Code:
Imports System.ComponentModel
Next, say we've built a business object "User" that contains data like first name, last name, phone, email, etc... We would like to call a function
that get's all users but as a bindable list of users so it could be directly bound to a textbox control.
Code:
Public Shared Function GetUsers() As BindingList(Of User)
Dim Users As BindingList(Of User)
' Hit the DAL
' Iterate Over All Users
Users.Add(New UserWeJustConstructedFromDBData)
Return Users
End Function Now, because this is a shared (static) method, their is no need to "instantiate" any objects, you can directly call the Method (Function) GetUsers. This binding list we just created is bindable to almost any control you can throw on the form or webpage.
Put this method in the actual User Class. To get this item as a data source in your form or aspx page code you would do:
Code:
Dim Users as BindingList(Of User) = User.GetUsers()
One line of code for a scalable binding list attachable to the majority of web or form controls.... I'd say it's a win-win
