2013-04-05 111 views
4

我有一个绑定,绑定,在VS2012中的窗体上,绑定到一个DateTimePicker控件绑定。DatePicker.Value.Set错误绑定到数据源

的结合性质我已经为MinDate = 1753年1月1日和MAXDATE = 31/12/9998 值已设置由从压延2013年5月4日上午11点27

我今天采摘设置绑定源使用

var dset = base.Context.ContactEvents; 
var qry = dset.Where(p => p.Id > 0).OrderBy(x => x.Id); 
qry.Load(); 
this.bindingSource.DataSource = dset.Local.ToBindingList(); 

绑定源以下列方式使用;

public void RefreshBindingDataSourceAndPosition(BindingSource binding) 
    { 
    binding.DataSource = this.bindingSource.DataSource; // error raised here 
    binding.Position = this.bindingSource.Position; 
    } 

错误信息是

System.ArgumentOutOfRangeException越过天然的/管理的边界 的HResult = -2146233086 消息= '1/01/0001 12:00:00 AM' 的值是对'价值'无效。 '价值'应该在'MinDate'和'MaxDate'之间。 参数名称:值 来源= System.Windows.Forms的 PARAMNAME =值 堆栈跟踪: 在System.Windows.Forms.DateTimePicker.set_Value(DateTime值) 的InnerException:

我可以解决这个问题通过不绑定数据选取器并将其设置在EventsBindingSource_CurrentChanged事件中

然而,必须这样做似乎很奇怪。我怎样才能让数据绑定工作?

[更新] 这个问题是一个类似于描述here 我试图重现该问题的一个简单的项目,以试图找出原因,但它工作在简单的项目。该项目也在另一台计算机上运行。 我的电脑上同时出现SQL Server 2012和2008R2问题。我试过改变控制面板中的日期格式和国家。此外,我已经尝试格式属性的不同设置。我也尝试设置日期字段以支持null。

当我将错误复制到剪贴板时,它显示以下内容;

System.Reflection.TargetInvocationException发生 的HResult = -2146232828 消息=异常已被调用的目标抛出。 源= mscorlib程序 堆栈跟踪: 在System.RuntimeMethodHandle.InvokeMethod(对象目标,对象[]参数,签名Sig,布尔构造函数) 在System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象OBJ,对象[]参数,对象[]参数) InnerException:System.ArgumentOutOfRangeException HResult = -2146233086 Message ='1/01/0001 12:00:00 AM'的值对'Value'无效。 '价值'应该在'MinDate'和'MaxDate'之间。 参数名称:值 源= System.Windows.Forms的 PARAMNAME =值 堆栈跟踪: 在System.Windows.Forms.DateTimePicker。SET_VALUE(DateTime值) 的InnerException:

我的EF类是如下

public class ContactEvent : LoggedEntity 
{ 

    public virtual SortableBindingList<ContactEventAttendee> Attendees { get; private set; } 
    public virtual ContactEventType ContactEventType { get; set; } 
    public string Details { get; set; } 
    public DateTime? EventTime { get; set; } 
    public virtual SortableBindingList<ContactEventItem> Items { get; private set; } 
    public int State { get; set; } 
    public string Title { get; set; } 
    public override string ToString() 
    { 
     return "Contact Events"; 
    } 
} 

它继承从

public abstract class LoggedEntity 
{ 

    public LoggedEntity() 
    { 
     this.RowId = Guid.NewGuid(); 
     this.RowVersionId = 0; 
     AppDomain dm = AppDomain.CurrentDomain; // Gets the current application domain for the current Thread. 
     object s = AppDomain.CurrentDomain.GetData("SiteNumber"); 
     this.SourceSiteNumber = Convert.ToInt32(s); 
    } 

    public LoggedEntity(int SiteNumber) 
    { 
     // the following 3 are used to identify the version 
     this.RowId = Guid.NewGuid(); 
     this.RowVersionId = 0; 
     this.SourceSiteNumber = SiteNumber; 
    } 

    public int Id { get; set; } 
    public Guid RowId { get; set; } 

    [ConcurrencyCheck] 
    public int RowVersionId { get; set; } 
    public int SourceSiteNumber { get; set; } 

} 

[更新] 类似的问题是here

[更新] 另一个here让我觉得我需要看看密钥是如何处理的。

[更新] 我注意到在输出窗口下面

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll 

[更新] 这使我 here

并打开调试选项后,我发现了一个错误

Invalid object name 'dbo.__MigrationHistory'. 

但是这是EF5中的已知错误

[更新]:我发现其他人有类似的未决问题​​ 发现我没有问题,运行.EXE时

[更新]我可以通过禁用“当异常跨应用程序域打破了错误跳过或管理/本地边界 在工具 - >选项 - >调试 - >一般

[更新]我添加以下内容,所以我可以检查控件属性。

private void EventsBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e) 
    { 
     // If the BindingComplete state is anything other than success, 
     // set the ErrorProvider to the error message. 
     if (e.BindingCompleteState != BindingCompleteState.Success) 
     { 
      errorProvider1.SetError((Control)e.Binding.BindableComponent, e.ErrorText); 
      var errdesc = e.ErrorText; 
      var ctrl = (Control)e.Binding.BindableComponent; 
       var info = string.Format(
       "{0} {1}",errdesc, 
       ctrl.ToString()); 

      Debug.Print(info); 
      // "Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'. 
      'Value' should be between 'MinDate' and 'MaxDate'.\r\nParameter name: 
      Value System.Windows.Forms.DateTimePicker, Value: 1/1/1900 12:00:00 AM" 

     } 
     else 
     { 
      errorProvider1.SetError((Control)e.Binding.BindableComponent, ""); 
     } 
    } 
+0

不 - 仍然把我的头发撕掉! – 2013-07-28 21:56:50

回答

0

看来,关键的问题是属性是可空的DateTime。可能用于转录DateTimePicker组件的空值是'1/01/0001 12:00:00 AM',这会产生与MinValue和MaxValue的配置有关的问题。使用DataBinding的高级选项卡可以选择将值用于null解决此问题的一种方法是将值设置为null作为MinDate,或者我们可以将MinDate设置为值'01/01/0001 12 :00:00 AM'。尽管我的经验有限,但我相信这可能是你问题的根源。链接http://msdn.microsoft.com/en-us/library/aa480734.aspx可以在属性DataBinding的高级选项卡上看到其他内容。

+0

只有我实际上没有任何数据库中的空日期。如果我更改要求的日期,问题仍然会发生 – 2013-08-04 07:30:56

+0

即使删除设置Min和MaxDate,问题也会发生? 另一种替代方法是在构造函数的实体类中为属性MinDate设置值。我希望我帮助 – 2013-08-04 20:04:43

+0

真正奇怪的是价值在范围内。我只是更新了这个问题。 – 2013-08-04 20:37:12

4

异常的原因可能是DatePicker的DataBinding“Value”属性已被设置为BindingSource字段。 只需要设置DatePicker的DataBinding“Text”属性以使数据绑定正常工作。 检查DatePicker的DataBinding“Value”属性字段中是否有值,一旦删除,问题应该消失。