I have a C# Windows Forms application which we originally wrote in VS2008 and have ported to VS2010. It has a binding between a object that we wrote with two date properties, bound to two datetime picker controls.
Here is the designer code, showing the bindings of one of the date time pickers; the other is identical except of course the name:
private System.Windows.Forms.DateTimePicker dtTradeDate;
this.dtTradeDate = new System.Windows.Forms.DateTimePicker();
//
// dtTradeDate
//
this.dtTradeDate.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bsWorkflowItem, "TradeDate", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.dtTradeDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
this.dtTradeDate.Location = new System.Drawing.Point(383, 43);
this.dtTradeDate.Name = "dtTradeDate";
this.dtTradeDate.Size = new System.Drawing.Size(99, 20);
this.dtTradeDate.TabIndex = 37;
The object properties are set up as get/set properties:
public DateTime TradeDate
{
get { return _tradeDate; }
set { _tradeDate = value; }
}
When the form starts up, we bind the binding source of the form to the object. You can see that I've added some explicit code to avoid the situation that is occurring below, but this has no effect:
//Initializing workflow item object
_wfItem = new CamraWorkflowItem(UserSession.User);
_wfItem.TradeDate = DateTime.Today;
_wfItem.EffectiveDate = DateTime.Today;
loading.IncrementLoadingSteps(2);
dtEffDate.Value = DateTime.Today;
dtTradeDate.Value = DateTime.Today;
bsWorkflowItem.DataSource = _wfItem;
Now here is the really, really strange part. This works perfectly well in VS2008 and also on two of my colleagues' VS2010 workstations. However it doesn't work on mine. When it hits the last line, I get the following exception:
System.ArgumentOutOfRangeException crossed a native/managed boundary
Message=Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'. Parameter name: Value
Source=System.Windows.Forms
ParamName=Value StackTrace: at System.Windows.Forms.DateTimePicker.set_Value(DateTime value) InnerException:
I'm at a loss here. If I disconnect both of the bindings, it works fine but obviously I need the binding to update the object. Also strangely if I look at the values of both of these dates in the debugger (with the exception box showing) I see a valid date; see below:
The date values on the object are the same, so they clearly appear to be within the valid date range of the control.
This seems like a bug in Visual Studio... except it works on my colleagues' machines with the same version of .NET and Visual Studio. I've spent a day hacking at this and I'm at a loss... your help is greatly appreciated.