Simple Binding and List Data Sources


A list data source is exactly what it sounds like: a collection of homogenous objects to be used as data in a binding operation. The minimum implementation that is considered a list data source by the Windows Forms binding engine is a class that implements the IList interface (from System.Collections). For convenience, you can use the generic IList implementation, List<T>, located in System.Collections.Generic, which is a strongly typed implementation of IList. For example, you could use List<T> to create a list of RaceCarDriver objects:

// SimpleBindingAndListDataSourcesForm.cs partial class SimpleBindingAndListDataSourcesForm : Form {   // Create strongly typed list data source   List<RaceCarDriver> raceCarDrivers = new List<RaceCarDriver>();   public SimpleBindingAndListDataSourcesForm() {     InitializeComponent();     // Populate list data source with data items     this.raceCarDrivers.Add(new RaceCarDriver("M. Schumacher", 500));     this.raceCarDrivers.Add(new RaceCarDriver("A. Senna", 1000));     this.raceCarDrivers.Add(new RaceCarDriver("A. Prost", 400));   } }


As you can see in this code, a list data source contains one or more item data sources (items). Although the items are contained within the list data source, we can still bind them to our Name and Wins text boxes:

// SimpleBindingAndListDataSourcesForm.cs partial class SimpleBindingAndListDataSourcesForm : Form {   ...   public SimpleBindingAndListDataSourcesForm() {     ...     // Bind the Name and Wins properties to the Name and Wins text boxes     this.nameTextBox.DataBindings.Add(       "Text", this.raceCarDrivers, "Name");     this.winsTextBox.DataBindings.Add(       "Text", this.raceCarDrivers, "Wins");   } }


This code should look familiar because it's almost exactly the same code used to bind our controls to an item data source. The only difference is in the object we specify as the data source; this time it's a list data source. When you simple-bind to a list data source like this, the controls default their values to the properties on the first item in the list, as Figure 16.4 demonstrates.

Figure 16.4. Controls Bound to a List Data Source and Displaying the First Item in the List


However, because TextBox controls support only simple binding, we can display the data for only one RaceCarDriver object at a time. To show more RaceCarDriver items, we need to add support to the form to navigate between them, as shown in Figure 16.5.

Figure 16.5. Navigating the Items in a Bound List Data Source


Navigation means changing the current item to another item in the list. The current item is special because it's managed by a special object known as a binding manager. Binding managers have the responsibility of managing a set of bindings for a particular data source and come in two flavors: property managers and currency managers. A property manager is an instance of the PropertyManager class and is created for an item data source. A currency manager is an instance of the CurrencyManager class and is created for a list data source. Both of these are implementations of the abstract base class BindingManagerBase:

namespace System.Windows.Forms {    abstract class BindingManagerBase {      // Constructor      public BindingManagerBase();      // Properties      public BindingsCollection Bindings { get; }      public abstract int Count { get; }      public abstract object Current { get; }      public bool IsBindingSuspended { get; } // New      public abstract int Position { get; set; }      // Methods      public abstract void AddNew();      public abstract void CancelCurrentEdit();      public abstract void EndCurrentEdit();      public virtual PropertyDescriptorCollection GetItemProperties();      public abstract void RemoveAt(int index);      public abstract void ResumeBinding();      public abstract void SuspendBinding();      // Events      public event BindingCompleteEventHandler BindingComplete; // New      public event EventHandler CurrentChanged;      public event EventHandler CurrentItemChanged; // New      public event BindingManagerDataErrorEventHandler DataError; // New      public event EventHandler PositionChanged;    }      ...      class PropertyManager : BindingManagerBase {...}      class CurrencyManager : BindingManagerBase {...} }


One of the jobs of a binding manager (both property manager and currency manager) is to keep track of the location of the current object, a task known as currency management. The current location is available from the binding manager's Position property. The Position property is always zero for a property manager, however, because it only manages a data source with a single item, as shown in Figure 16.6.

Figure 16.6. A Property Manager Maintaining Currency on an Item Data Source


For a currency manager, however, the position is an index into the list of items in the list data source, as shown in Figure 16.7.

Figure 16.7. A Currency Manager Maintaining Currency on a List Data Source


To implement navigation for a list data sourcedoing so for an item data source doesn't make senseyou acquire the BindingManager for the desired data source and use it to change the binding manager's position as appropriate:

// SimpleBindingAndListDataSourcesForm.cs partial class SimpleBindingAndListDataSourcesForm : Form {   ...   BindingManagerBase BindingManager {     get { return this.BindingContext[this.raceCarDrivers]; }   }   void moveFirstButton_Click(object sender, EventArgs e) {     this.BindingManager.Position = 0;   }   void movePreviousButton_Click(object sender, EventArgs e) {     // No need to worry about being < 0     --this.BindingManager.Position;   }   void moveNextButton_Click(object sender, EventArgs e) {     // No need to worry about being > BindingManager.Count     ++this.BindingManager.Position;   }   void moveLastButton_Click(object sender, EventArgs e) {     this.BindingManager.Position = this.BindingManager.Count - 1;   } }


Here, the code implements a property that provides access to the binding manager for the RaceCarDriver's list data source to which the Text properties of the Name and Wins text boxes are bound. Then, the code simply updates the binding manager's position as appropriate. The change in position causes the bindings of all bound controls to update to the new current object.

As a useful visual aid, we can relay to the user the location of the current item with respect to the total number of items, as well as enable or disable the move buttons as we reach either end of the list. Both functions rely on displaying the BindingManager's Position and Count properties when the form loads and when the current item changes:

// SimpleBindingAndListDataSourcesForm.cs partial class SimpleBindingAndListDataSourcesForm : Form {   ...   public SimpleBindingAndListDataSourcesForm() {     ...     RefreshItems();   }   ...   void moveFirstButton_Click(object sender, EventArgs e) {     this.BindingManager.Position = 0;     RefreshItems();   }   void movePreviousButton_Click(object sender, EventArgs e) {     // No need to worry about being < 0     --this.BindingManager.Position;     RefreshItems();   }   void moveNextButton_Click(object sender, EventArgs e) {     // No need to worry about being > BindingManager.Count     ++this.BindingManager.Position;     RefreshItems();   }   void moveLastButton_Click(object sender, EventArgs e) {     this.BindingManager.Position = this.BindingManager.Count - 1;     RefreshItems();   }   ...   void RefreshItems() {     int count = this.BindingManager.Count;     int position = this.BindingManager.Position + 1;     // Update count and position text     this.countLabel.Text = count.ToString();     this.positionLabel.Text = position.ToString();     // Enable or disable move buttons     this.moveFirstButton.Enabled = (position > 1);     this.movePreviousButton.Enabled = (position > 1);     this.moveNextButton.Enabled = (position < count);     this.moveLastButton.Enabled = (position < count);   } }


The result is shown in Figure 16.8.

Figure 16.8. Displaying Position and Count


Although simple binding works just fine for list data sources, it's quite likely you'll want to use list data sources with controls that can show more than one object at a time, such as ListBox and DataGridView. For this, we have complex binding.




Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net