Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

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

Windows Forms FAQs

Why doesn't DataSourceUpdateMode.OnPropertyChanged work for ADO.NET data sources?

When binding to ADO.NET tables using simple list binding, the Windows Forms runtime binds to an instance of an ADO.NET DataRowView.  The DataRowView supports row level commit semantics via an interface called IEditableObject (see the IEditableObject section for more information).  IEditableObject requires changes to be "committed" prior to the changes being accepted by the data model.  When binding to the DataRowView, the DataRowView will update as a result of data binding however it defers change notification until the row has been committed.  What this means is that even though the DataRowView has been updated, other controls are not aware of the update and therefore don't show the updated value.  You can work around this by either manually forcing other bound controls to refresh their contents or by explicitly calling EndEdit on the DataRowView.

Sample: Explicitly calling EndEdit with simple binding (VS 2005) (VS Project: OnPropertyChangedBinding)

In VS 2005, a new event named BindingComplete was added to the Binding type to provide general information on the completion state of a binding operation.  When using simple binding, you can use this event to call EndEdit to automatically commit changes on an instance that supports IEditableObject (e.g. ADO.NET DataRowView).

/* 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);

/* Get the binding – could have manually created and added the binding as well */
Binding adoBinding = cb.DataBindings["Checked"];

/* Force EndEdit for ADO.NET */
adoBinding.BindingComplete += delegate(object binding, BindingCompleteEventArgs args)
{
    if ((args.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate) &&
        (args.BindingCompleteState == BindingCompleteState.Success))
    {
        DataRowView drv = (args.Binding.BindingManagerBase.Current as DataRowView);

        /* Force ADO.NET to commit the value */
        if (null != drv)
        {
            drv.EndEdit();
        }
    }
};


Page view counter