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 get a CheckBox control to immediately update the data source when its value changes?

By default, simple binding updates a bound data source property value as part of Control validation.  Control validation occurs when a Control loses focus (see Controlling the Binding Operation for more information).  In VS 2005, a new property named DataSourceUpdateMode was added to the Binding type to provide greater flexibility over when a control updates a bound data source property.   When this property is set to OnPropertyChanged, it will cause the data source to get updated when the control property value changes rather than on validation (focus change).  Note that this only works for control properties that support change notification in the form of the "PropertyName"Changed pattern (see Property Change Notification for more information). 

Sample: Updating a data source property when a control property changes (VS 2005) (VS Project: OnPropertyChangedBinding)

You can set the DataSourceUpdateMode for a binding using the VS 2005 designer using the "Formatting and Advanced Binding" dialog.  You can set this value in code as shown below:

/* Bind the CheckBox.Checked property to the data source property "Prop" */
/* Update the data source when the Checked property value changes */
cb.DataBindings.Add("Checked", ds, "Prop", true, DataSourceUpdateMode.OnPropertyChanged);