Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding to a Nullable<DateTime> control property
    text
    copied!<p>We have a custom control that has a "Value" property of type System.Nullable (aka System.DateTime?). We have an object with a "Received" property of the same type. When we try to bind the control to the object, the following <em>InvalidCastException</em> is thrown:</p> <p><strong>Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.</strong></p> <p>Here is what we're doing:</p> <p>Object property:</p> <pre><code>private System.DateTime? _dateTimeReceived; public System.DateTime? DateTimeReceived { get { return this._dateTimeReceived; } set { this._dateTimeReceived = value; this.OnChanged("DateTimeReceived", value); //Implements INotifyPropertyChanged and fires PropertyChanged event } } </code></pre> <p>Control property:</p> <pre><code>private System.DateTime? _value; [System.ComponentModel.Category("Behavior")] [System.ComponentModel.Description("The current date value for this control")] public new System.DateTime? Value { get { return this._value; } set { this._value = value; } } </code></pre> <p>In the application, here is where the exception is thrown:</p> <pre><code>this.dateReceived.DataBindings.Add("Value", this._object, "DateTimeReceived"); </code></pre> <p>As you can see, the object's property (this._object.DateTimeReceived) is a System.DateTime? type and the control's property (this.dateReceived.Value) is a System.DateTime? type.</p> <p>Why would this cause an <em>InvalidCastException</em>? And how can we correct this so that it binds correctly?</p> <p><strong>Update 2009-10-29 14:26 CDT:</strong></p> <p>Here is the stack trace:</p> <blockquote> <p>at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)<br> at System.DateTime.System.IConvertible.ToType(Type type, IFormatProvider provider) <br> at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) <br> at System.Windows.Forms.Binding.FormatObject(Object value)<br> at System.Windows.Forms.Binding.PushData(Boolean force) <br> at System.Windows.Forms.Binding.UpdateIsBinding() <br> at System.Windows.Forms.Binding.CheckBinding() <br> at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) <br> at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding) <br> at System.Windows.Forms.BindingsCollection.Add(Binding binding) <br> at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)<br> at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)<br> at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding) <br> at System.Windows.Forms.BindingsCollection.Add(Binding binding) <br> at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember, Boolean formattingEnabled, DataSourceUpdateMode updateMode, Object nullValue, String formatString, IFormatProvider formatInfo) <br> at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember)<br></p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload