【发布时间】:2011-03-11 14:46:27
【问题描述】:
我正在开发 .NET 4.0 中的 Windows 窗体应用程序。当我将数据绑定到 BindingSource(绑定了 ComboBox)时,出现以下异常。注意:只有当我让调试器在抛出异常时停止,无论是未处理的还是已处理的,我都会得到它。因此,异常在某处被捕获 - 但是我不确定是否可以被抛出。
发生 ArgumentOutOfRangeException InvalidArgument=“0”的值对“SelectedIndex”无效。 参数名称:SelectedIndex
我没有设置SelectedIndex 属性。我的代码如下所示。 myData 是 IList 的实体(List 在运行时):
myBindingSource.DataSource = myData;
我不知道我做错了什么。此外,调用堆栈让我有点困惑(见下文)。 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 项。如果你的数据源中没有项目,你为什么不做检查不绑定?