InvalidArgument =值'0'对'SelectedIndex'无效

Mat*_*eid 8 .net c# winforms

我正在使用.NET 4.0中的Windows窗体应用程序.当我将数据绑定到a BindingSource(绑定到a ComboBox)时,我得到以下异常.注意:只有当调试器停止在被抛出的异常时才会得到它,无论是未处理还是处理.因此,异常被捕获到某处 - 但是我不确定是否可以抛出它.

ArgumentOutOfRangeException发生 InvalidArgument =值'0'对'SelectedIndex'无效.参数名称:SelectedIndex

我没有设置SelectedIndex财产.我的代码如下所示.myData是一个IList实体(List在运行时):

myBindingSource.DataSource = myData;
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚我做错了什么.而且,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)
Run Code Online (Sandbox Code Playgroud)

Dan*_*rth 16

当您要求调试器停止在Exceptions上时,它将执行此操作,无论它们是否将被处理.这会导致像您观察到的情况:
调试器在异常处停止并使您感到困惑,尽管异常完全有效并且似乎是周围代码所期望的,因为它处理异常而不会死亡.

总结并回答您的问题:
并非调试器停止的所有异常都表明您正在做错事或代码中存在问题.

更新(信用转到标记):
如果启用"仅我的代码"选项,您可以告诉调试器仅捕获异常.