【发布时间】:2013-03-27 06:52:45
【问题描述】:
我在 VS2012 的一个窗体上有一个名为 binding 的 bindingsource 控件,并绑定了一个 DateTimePicker 控件。
对于绑定属性,我有 MinDate = 1/01/1753 和 MaxDate = 31/12/9998 值已通过从日历中选择 Today 来设置 5/04/2013 11:27 AM
我使用
设置了绑定源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();
bindingsource的使用方式如下;
public void RefreshBindingDataSourceAndPosition(BindingSource binding)
{
binding.DataSource = this.bindingSource.DataSource; // error raised here
binding.Position = this.bindingSource.Position;
}
错误信息是
System.ArgumentOutOfRangeException 越过了本机/托管边界 H结果=-2146233086 消息=“1/01/0001 12:00:00 AM”的值对于“值”无效。 “值”应介于“MinDate”和“MaxDate”之间。 参数名称:值 源=System.Windows.Forms 参数名称=值 堆栈跟踪: 在 System.Windows.Forms.DateTimePicker.set_Value(日期时间值) 内部异常:
我可以通过不绑定 Data Picker 并将其设置在 EventsBindingSource_CurrentChanged 事件中来解决此问题
但是,必须这样做似乎很奇怪。我怎样才能让数据绑定工作?
[更新] 这个问题和here描述的类似 我试图在一个更简单的项目中重现该问题,以尝试找出原因,但是它在更简单的项目中有效。该项目也可以在另一台计算机上运行。 我的计算机同时使用 SQL Server 2012 和 2008R2 时出现此问题。我尝试在控制面板中更改日期格式和国家/地区。我也尝试了 format 属性的不同设置。我也尝试将日期字段设置为支持 null。
当我将错误复制到剪贴板时,它会显示以下内容;
System.Reflection.TargetInvocationException 发生 H结果=-2146232828 Message=Exception 已被调用的目标抛出。 源=mscorlib 堆栈跟踪: 在 System.RuntimeMethodHandle.InvokeMethod(对象目标,对象 [] 参数,签名 sig,布尔构造函数) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象 obj,对象 [] 参数,对象 [] 参数) 内部异常:System.ArgumentOutOfRangeException H结果=-2146233086 消息=“1/01/0001 12:00:00 AM”的值对于“值”无效。 “值”应介于“MinDate”和“MaxDate”之间。 参数名称:值 源=System.Windows.Forms 参数名称=值 堆栈跟踪: 在 System.Windows.Forms.DateTimePicker.set_Value(日期时间值) 内部异常:
我的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 中的一个已知错误
[更新]:我找到了另一个有类似未解决问题的人here 发现我在运行.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, ""); } }
【问题讨论】:
-
不 - 还在扯我的头发!
标签: entity-framework-5 datetimepicker