簡體   English   中英

InvalidArgument =值'0'對'SelectedIndex'無效

[英]InvalidArgument=Value of '0' is not valid for 'SelectedIndex'

我正在使用.NET 4.0中的Windows窗體應用程序。 當我將數據綁定到BindingSource (綁定ComboBox )時,我得到以下異常。 注意:只有當調試器停止在被拋出的異常時才會得到它,無論是未處理還是處理。 因此,異常被捕獲到某處 - 但是我不確定是否可以拋出它。

ArgumentOutOfRangeException發生 InvalidArgument =值'0'對'SelectedIndex'無效。 參數名稱:SelectedIndex

我沒有設置SelectedIndex屬性。 我的代碼如下所示。 myData是實體的IList (運行時List ):

myBindingSource.DataSource = myData;

我無法弄清楚我做錯了什么。 而且,Call Stack讓我感到困惑(見下文)。 Windows窗體框架似乎在組合框上設置SelectedIndex ,這會導致異常。 有人知道擺脫這個的方法嗎?

干杯馬蒂亞斯

System.Windows.Forms.dll!System.Windows.Forms.ComboBox.SelectedIndex.set(int value) + 0x233 bytes   
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x3e bytes    
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0x1bd bytes    
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x75c bytes   
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.ResetBindings(bool metadataChanged) + 0x3e bytes    
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.SetList(System.Collections.IList list, bool metaDataChanged, bool applySortAndFilter) + 0x22c bytes 
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.DataSource.set(object value) + 0x47 bytes   
(my method)

當您要求調試器停止在Exceptions上時,它將執行此操作,無論它們是否將被處理。 這會導致您觀察到的情況:
調試器在異常處停止並使您感到困惑,盡管異常完全有效並且似乎是周圍代碼所期望的,因為它處理異常而不會死亡。

總結並回答您的問題:
並非調試器停止的所有異常都表明您正在做錯事或代碼中存在問題。

更新(積分轉到馬克):
如果啟用“僅我的代碼”選項,則可以告訴調試器僅捕獲異常。

你也可以試試這個。 在設置組合框之前,DataSource設置其BindingContext

myBindingSource.BindingContext = this.BindingContext;
myBindingSource.DataSource = myData;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM