Data Binding Overview

Data binding is the act of attaching a source of data to a user interface control. As you will see, data binding can be done in a read-only mode or in a bi-directional mode in which data can flow from the source to the control and data can flow from the control (via the user) to the data source. The next section will introduce you to some of the basic concepts of WinForms data binding and show you the various ways in which data binding can be done on a Windows Form. If you have any preconceived notions about data binding based on your COM, Visual Basic 6, or MFC experience, you should leave those behind.

Introduction to Windows Forms Data Binding

Windows Forms data binding provides an extremely powerful, easy-to-use method for linking data between a data source and a GUI control. The controls that are typically bound are DataGrids, ComboBoxes, TextBoxes, and many more. They can be bound to all kinds of data, including objects that implement the IList interface, arrays, ArrayLists, DataSets, DataTables, and much more. As you will see later in this chapter, you can even bind different properties of controls (such as size, position, color, and so on) to different data sources.

Simple Data Binding

Simple data binding refers to a situation in which a single value within the data source is bound to a single property on the interface control. For example, you can bind a value in the data source to the Text property of a TextBox. In other cases, you might want to bind some of the numeric properties of a control to data source values, such as the size, position, or any other aspect of the control that can be managed with a single property.

The ability to bind the value of a property to a single value within a data source is accomplished through the DataBindings collection. Every control that can be bound has a DataBindings collection. When you add a binding to this collection, you specify the property on the control to which you will be binding. Then you specify the object that provides the binding value and the property name on that object that will supply the value. Listing 30.1 shows how to use the DataBindings collection to create two different controls that have their Text properties dynamically bound to the TextProperty property on the SimpleBindingClass class instance.

To create the sample shown in Listing 30.1, just create a new Windows Forms application called SimpleBinding and change the main form class to be called frmMain.cs.

Listing 30.1. The Main Form of a Simple Data Binding Demonstration Application
    using System;    using System.Drawing;    using System.Collections;    using System.ComponentModel;    using System.Windows.Forms;    using System.Data;    namespace SimpleBinding    {      public class frmMain : System.Windows.Forms.Form      {        private System.Windows.Forms.Label lblSimpleBinding;        private System.Windows.Forms.TextBox txtSimpleBinding;        /// <summary>        /// Required designer variable.        /// </summary>        private System.ComponentModel.Container components = null;        private System.Windows.Forms.Button btnExamine;        private SimpleBindingClass simpleBind = new SimpleBindingClass();        public frmMain()        {          InitializeComponent();          simpleBind.TextProperty = "Hello World";          lblSimpleBinding.DataBindings.Add("Text", simpleBind, "TextProperty");          txtSimpleBinding.DataBindings.Add("Text", simpleBind, "TextProperty");        }        /// <summary>        /// Clean up any resources being used.        /// </summary>        protected override void Dispose( bool disposing )        {          if( disposing )          {            if (components != null)            {              components.Dispose();            }         }         base.Dispose( disposing );        }    // Windows Forms designer code cut for ease of reading      /// <summary>      /// The main entry point for the application.      /// </summary>      [STAThread]      static void Main()      {        Application.Run(new frmMain());      }      private void btnExamine_Click(object sender, System.EventArgs e)      {        MessageBox.Show(simpleBind.TextProperty);      }     }    } 

The most important lines of code are the following lines taken from the form's constructor :

 simpleBind.TextProperty = "Hello World"; lblSimpleBinding.DataBindings.Add("Text", simpleBind, "TextProperty"); txtSimpleBinding.DataBindings.Add("Text", simpleBind, "TextProperty"); 

The preceding code binds both UI controls' Text property to the simpleBind class's TextProperty property. One important thing to note is that the mechanism performing the binding will not update the source of the data (in this case, the simpleBind class instance) until you tab out of the text box. In other words, the code will not waste time trying to update bindings every time a character changes in the TextBox control.

Figure 30.1 shows the preceding application in action, after having changed the text box value and allowing the text box to lose focus.

Figure 30.1. The simple data binding sample application.

Complex Data Binding

Complex data binding involves binding a user interface control to a list of data instead of a single, scalar value. This list can be virtually any list of data, including an array, an ArrayList, Hashtables, and so forthvirtually anything that implements the IList interface. You will see more about binding to complex data structures later in this chapter.

One-Way and Two-Way Data Binding

As mentioned earlier, Windows Forms allow both one and two-way data binding. When you want to accomplish one-way data binding, you bind the user interface controls to some object that does not propagate changes back to the original data source. This enables you to feed the user interface the data that you want, as well as to explicitly control what data does and does not make it back into the database, text file, XML source, or whatever your data source might be. Two-way data binding refers to associating the user interface control with an object that propagates its changes back to the data source (such as the dynamically bound DataSet/DataAdapter combination demonstrated in Chapter 27, "Using .NET Data Providers").

    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316 © 2008-2017.
    If you may any questions please contact us: