title: How to bind a CheckBox to a bool typed DbColumn that is nullable?
body: <p>In Windows Forms (.NET 2.0, Visual Studio 2005 SP1) : I have a typed <code>DataSet</code>, with a column which type is <code>System.Boolean</code>, which is nullable and which default value is <code>DBNull</code>. I have a <code>Form</code>, containing a <code>CheckBox</code> control that I want to bind to the prior column value.</p>
  
  <ul>
  <li>I have tried to bind the <code>Checked</code> property to the column via the designer : it works great, only if the default value for the column is set to either <code>True</code> or <code>False</code>.</li>
  <li><p>I have tried to bind the <code>CheckState</code> property to the column via the designer, and attaching my own <code>Format</code> and <code>Parse</code> event handlers but they never get called :</p>
  
  <pre><code>b.Format+=delegate(object sender, ConvertEventArgs cevent) {
      cevent.Value=DoFormat((CheckState)cevent.Value); // cf. end of the question
  };
  b.Parse+=delegate(object sender, ConvertEventArgs cevent) {
      cevent.Value=DoParse(cevent.Value); // cf. end of the question
  };
  </code></pre></li>
  <li><p>I have tried to create a custom <code>Binding</code> instance in the code, attach my event handlers and add it to the <code>CheckBox</code> bindings : the event handlers are still never get called...</p>
  
  <pre><code>Binding b=new Binding("CheckState", _BindingSource, "MyColumn", false, DataSourceUpdateMode.OnPropertyChanged, DBNull.Value);
  </code></pre></li>
  </ul>
  
  <p>As a note : a <code>DBNull</code> value is acceptable only when coming from the <code>DataSet</code> (it means the value has never been set). But the user should only be able to set the value to <code>True</code> or <code>False</code> via the <code>CheckBox</code>.</p>
  
  <p>For reference, here is the code of the parsing and formatting methods :</p>
  
  <pre><code>internal static CheckState DoParse(object value)
  {
      if ((value==null) || (value is DBNull))
          return CheckState.Indeterminate;
  
      bool v=Convert.ToBoolean(value);
      return (v ? CheckState.Checked : CheckState.Unchecked);
  }
  
  internal static object DoFormat(CheckState value)
  {
      switch (value)
      {
      case CheckState.Checked:
          return true;
      case CheckState.Indeterminate:
          return DBNull.Value;
      case CheckState.Unchecked:
          return false;
      }
  
      return null;
  }
  </code></pre>