title: C# winforms: Error when binding nullable type to other property (not Text)
body: <p>I've made a custom Textbox which displays a (nullable) decimal value as time (2.5 = 2:30). I've added a property "decimal? DecimalValue" to which I bind my datasource. This all works fine, except when I clear the Textbox, to make the value null. </p>
  
  <p>It says "Object of type System.String cannot be converted to System.Nullable`1[System.Decimal]". </p>
  
  <p>What I saw is that base.OnValidating makes e.Cancel = True. So, somewhere underneath there is a check performed that causes this. I don't understand this behaviour though, because when I bind to property Text I can clear the Textbox without a problem and a null value is being saved. </p>
  
  <p>Code of binding:</p>
  
  <pre><code>this.txtUrenDoorberekenen.DataBindings.Add(new System.Windows.Forms.Binding("DecimalValue", this.bsAutokraanOrderRegel, "Uren", true));
  </code></pre>
  
  <p>Custom property:</p>
  
  <pre><code>[Browsable(true), Bindable(true), Category("DSE"), DefaultValue(null), Description("De numerieke waarde")]
  public decimal? DecimalValue
  {
      get { return this.GeefNumeriek(this.Text); }
      set { this.Text = this.GeefTijd(value); }
  }
  </code></pre>
  
  <p>this.GeefNumeriek return a decimal? (it converts the Text of Textbox to a nullable decimal).
  this.GeefTijd(value) converts a nullable decimal to a string format. </p>
  
  <pre><code>    private decimal? GeefNumeriek(string waarde)
      {
          decimal? result = null;
          if (!String.IsNullOrEmpty(waarde)) {
              try {
                  // voeg dubbele punt toe, indien deze ontbreekt
                  if (waarde.Length == 1 &amp;&amp; waarde.IndexOf(":") &lt; 0) { waarde = waarde + ":00"; }
                  if (waarde.Length == 2 &amp;&amp; waarde.IndexOf(":") &lt; 0) { waarde = waarde + ":00"; }
                  if (waarde.Length &gt;= 3 &amp;&amp; waarde.IndexOf(":") &lt; 0) { waarde = waarde.Substring(0, waarde.Length - 2) + ":" + waarde.Substring(waarde.Length - 2); }
  
                  // Uren gedeelte
                  result = Convert.ToDecimal(waarde.Substring(0, waarde.IndexOf(":")));
  
                  // Minuten
                  int minuten = Convert.ToInt16(waarde.Substring(waarde.IndexOf(":") + 1));
                  // Minuten kan niet meer dan 60 zijn
                  if (minuten &gt; 60) { throw new Exception(DSETextResource.GeefText("Validatie_Numeriek_Ongeldig")); }
  
                  result = result + ((decimal)minuten / (decimal)60);
              }
              catch {
              }
          }
          return result;
      }
  </code></pre>