title: Winforms databinding and validation, why is datasource updated when validation fails?
body: <p>The code below illustrates some unexpected (for me!) behaviour when combining data binding and validation in winforms. Can anyone tell me how I can prevent the datasource from being updated when validation fails?</p>
  
  <p>Many thanks.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace ValidationBug
  {
      /// &lt;summary&gt;
      /// This illustrates some unexpected behaviour with winforms validation and binding
      /// 
      /// To reproduce: Run the program, enter a value into the textbox, click the X to close the form.
      /// 
      /// Expected behaviour: validation of textbox fails so data source is not updated.
      /// 
      /// Observed behaviour: data source is updated.
      /// &lt;/summary&gt;
      public class Form1 : Form
      {
          private class Data
          {
              private string _field;
              public string Field
              {
                  get { return _field; }
                  set 
                  { 
                      // this should never be called, but it is.
                      _field = value; 
                  }
              }
          }
  
          private System.ComponentModel.IContainer components = null;
  
          public Form1()
          {
              this.Load += new System.EventHandler(this.Form1_Load);
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              AutoValidate = System.Windows.Forms.AutoValidate.EnablePreventFocusChange;
  
              var txt = new TextBox();
  
              // validation always fails.
              txt.Validating += new CancelEventHandler((s, ev) =&gt; ev.Cancel = true);
              Controls.Add(txt);
  
              var data = new Data();
  
              this.components = new System.ComponentModel.Container();
              BindingSource bs = new BindingSource(this.components);
              bs.DataSource = typeof(Data);
  
              // only update datasource on succesful validation.
              txt.DataBindings.Add(new Binding("Text", data, "Field", false, DataSourceUpdateMode.OnValidation));
          }
      }
  }
  </code></pre>