Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Databind Objects and Collections
Take advantage of the fact that the WinForms databinding mechanism can use any .NET object that implements the IList interface as the data source.
by Francesco Balena

February 10, 2005

Technology Toolbox: VB.NET

Databinding is a key feature of the Microsoft .NET Framework, both in its Windows Forms and Web Forms flavors. Many .NET developers are quite familiar with Windows Forms databinding, especially those with previous VB6 experience; therefore, most articles and books focus on databinding in ASP.NET applications. Nevertheless, many facets of Windows Forms databinding aren’t widely understood among developers. For example, many developers fail to appreciate how useful databinding can be even when they’re not working with information stored in a database.

ADVERTISEMENT

Developers typically use databinding in conjunction with ADO.NET objects, such as DataSet and DataView objects, but the Windows Forms databinding mechanism can use any .NET object that implements the IList interface as the data source, whether an array, an ArrayList, or a custom collection. Assume you have a Person class that exposes properties such as FirstName, LastName, BirthDate, and Married. You can create an array or a collection of Person objects and bind it to controls like this:

Dim WithEvents manager As _
   CurrencyManager

Sub InitializeBindings()
   ' persons is the array of Person 
   ' objects
   manager = _
      CType(Me.BindingContext( _
      persons), CurrencyManager)
   txtFirstName.DataBindings.Add( _
      "Text", persons, "FirstName")
   txtLastName.DataBindings.Add( _
      "Text", persons, "LastName")
   txtBirthDate.DataBindings.Add( _
      "Text", persons, "BirthDate")
   chkMarried.DataBindings.Add( _
      "Checked", persons, "Married")
   UpdateControlState()
End Sub

The InitializeBindings procedure is invoked before the form becomes visible, typically in the Form_Load event. Its first statement assigns a value to the form-level manager variable; you can use the CurrencyManager.Position property to navigate through all the rows in the data source. The UpdateControlState procedure tests this property and enables or disables the navigation buttons accordingly:

Sub UpdateControls()
   btnFirst.Enabled = _
      manager.Position > 0
   btnPrevious.Enabled = _
      manager.Position > 0
   btnNext.Enabled = _
      manager.Position < _ 
      manager.Count-1
   btnLast.Enabled = _
      manager.Position < _
      manager.Count-1
   lblRecord.Text = _
      String.Format("{0} of {1}", _
      manager.Position + 1, _
      manager.Count)
End Sub



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home