Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I control the binding operation?

The Windows Forms simple binding type (System.Windows.Forms.Binding) allows developers to control how and when a change to user interface element updates a bound data source property as well as how and when a change to a data source property updates the user interface element.  For example, if a developer bound the "Name" property on a Customer instance to the "Text" property on a TextBox control, the developer can specify when changes to the user interface element (Control) are propagated to the data source.  Currently supported options are during validation of the TextBox (when user Tabs off the TextBox – this is the default), when any changes are made to the Text value or never.  The developer can also control when data source changes update the bound user interface element.  Currently supported options are when the data source property changes (the default) and never.  Note that developers can use the combination of "never" and calling the Binding API (ReadValue/WriteValue) to provide their own explicit rules for synchronizing data between the data source property and the user interface element.

/*******************************************************************
* Setup (using the Visual Studio Form designer):
* 
*  Add 3 TextBoxes to the Form (textBox1, textBox2 and textBox2)
*  Add an ErrorProvider to the Form (errorProvider1)
*  Add a Button to the Form (button1)
*  Add the code below to the Form.Load event
******************************************************************/

/*******************************************************************
* DataSource setup:
* 
* Create a Table named Numbers and add 3 columns
*  ID:     int
*  Name:   string
*  Cost:   Decimal
******************************************************************/

DataTable _dt;

_dt = new DataTable("Numbers");

_dt.Columns.Add("ID", typeof(int));
_dt.Columns.Add("Name", typeof(string));
_dt.Columns.Add("Cost", typeof(decimal));

_dt.Rows.Add(0, "Zero", 10.0M);
_dt.Rows.Add(1, "One", 11.1M);
_dt.Rows.Add(2, "Two", 12.2M);

/*******************************************************************
* Set up the ErrorProvider:
* 
* Bind it to the same data source used by the TextBoxes.
******************************************************************/

this.errorProvider1.DataSource = _dt;

/*******************************************************************
* Bind TextBox.Text (string) to Numbers.ID (integer)
*
* Set DataSourceUpdateMode to OnPropertyChanged. This will
* cause the changes in the TextBox.Text value to be immediately
* set to the Data Source property.
*
* Note, in this mode the ErrorProvider will immediately display an
* error rather than display only an error when the user Tabs
* off the TextBox.
******************************************************************/ 
       
this.textBox1.DataBindings.Add("Text", _dt, "ID", true, 
    DataSourceUpdateMode.OnPropertyChanged);

/*******************************************************************
* Bind TextBox.Text to Form.Size (in this scenario, Form is the data source)
* Do not update the DataSource (Form) with Control changes
******************************************************************/

Binding sizeBinding = new Binding("Text", this, "Size", true, 
             DataSourceUpdateMode.Never);
this.textBox2.DataBindings.Add(sizeBinding);

/*******************************************************************
* Setup the Button.Click to explicitly update the DataSource (Form.Size).
*
* Use anonymous delegate to keep the code cleaner
******************************************************************/
 
this.button1.Click += delegate(object button, EventArgs args)
{
sizeBinding.WriteValue();
};


Page view counter