DatePicker.Value.Set错误绑定到数据源

Kir*_*eed 4 datetimepicker entity-framework-5

我在VS2012中的表单上有一个名为binding的bindingsource控件,并且有一个绑定到它的DateTimePicker控件。

对于绑定属性,我具有MinDate = 1/01/1753和MaxDate = 31/12/9998值是通过从压延机中选择“今天”设置的。2013年5月4日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();
Run Code Online (Sandbox Code Playgroud)

绑定源的使用方式如下:

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

错误信息是

System.ArgumentOutOfRangeException越过了本机/托管边界HResult = -2146233086 Message =值'1/01/0001 12:00:00 AM'对'值'无效。“值”应介于“ MinDate”和“ MaxDate”之间。参数名称:值Source = System.Windows.Forms ParamName =值StackTrace:在System.Windows.Forms.DateTimePicker.set_Value(DateTime值)内部异常:

我可以通过不绑定数据选择器并将其设置在EventsBindingSource_CurrentChanged事件中来解决此问题。

但是,这样做似乎很奇怪。如何使数据绑定正常工作?

[更新]此问题与此处描述的问题类似, 我试图在一个较简单的项目中重现此问题,以便尝试找出原因,但是它在较简单的项目中有效。项目也可以在另一台计算机上工作。在同时使用SQL Server 2012和2008R2的计算机上会发生此问题。我尝试在控制面板中更改日期格式和国家/地区。我也尝试了format属性的不同设置。我也尝试将日期字段设置为支持null。

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

发生System.Reflection.TargetInvocationException HResult = -2146232828 Message = Exception已由调用目标抛出。Source = mscorlib StackTrace:位于System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object []参数,Object []参数)的System.RuntimeMethodHandle.InvokeMethod(对象目标,Object []参数,签名sig,布尔构造函数)InnerException: System.ArgumentOutOfRangeException HResult = -2146233086消息=值'1/01/0001 12:00:00 AM'对于'值'无效。“值”应介于“ MinDate”和“ MaxDate”之间。参数名称:值Source = System.Windows.Forms ParamName =值StackTrace:位于System.Windows.Forms.DateTimePicker。

我的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";
    }
}
Run Code Online (Sandbox Code Playgroud)

它从

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; }

}
Run Code Online (Sandbox Code Playgroud)

[更新]类似的问题在这里

[更新]另外一个在这里让我觉得我需要看看密钥如何被处理。

[更新]我在输出窗口中注意到以下内容

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Run Code Online (Sandbox Code Playgroud)

[更新]这导致我来到 这里

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

Invalid object name 'dbo.__MigrationHistory'.
Run Code Online (Sandbox Code Playgroud)

但是,这是EF5中的已知错误

[更新]:我在这里找到另一个具有类似未解决问题的人 发现运行.EXE时我没有问题

[更新]我可以通过在“工具”->“选项”->“调试”->“常规”中禁用“当异常跨越App Domain或托管/本机边界时中断,从而跳过错误

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

    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, "");
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

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