2013-04-05 63 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」屬性字段中是否有值,一旦刪除,問題應該消失。