View Single Post
  #1 (permalink)  
Old 06-25-2007, 11:01 PM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 317
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Icon11 VB.NET 2005 Daily Lesson [6.26.07] - Building Bindable Objects

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
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
HelloWorld (06-25-2007)