我正在使用.NET 4.0中的Windows窗体应用程序。因为我将数据绑定到BindingSource
(绑定了ComboBox
),所以我得到以下例外。注意:只有在调试器停止在抛出异常的情况下,我才能得到它,不管是未处理还是处理。因此,这个例外被捕获 - 但我不确定是否可以抛出。InvalidArgument ='0'的值对'SelectedIndex'无效
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)
当你绑定你的组合时,它会自动将selectedIndex设置为第0项。如果数据源中没有项目,为什么不做检查并且不绑定? – Pabuc