当我尝试选择通过绑定源添加到列表框中的新项目时,出现此异常。ListBox在BindingSource更新后抛出ArgumentOutOfRangeException
这是自定义的数据源的BindingSource的对象
BindingSource bs = new BindingSource() { DataSource = myObjectsList };
listbox.DataSource = bs;
listbox.DisplayMember = "MyObjectProperty";
当我添加一个新的项目到BindingSource的列表框更新,但我不能选择新项目
bs.Add(new MyObject());
int newItemIndex = listbox.Items.Count - 1; // this returns the right index of the new added item
listbox.SelectedIndex = newItemIndex;
这里我得到了System.ArgumentOutOfRangeException(InvalidArgument ='0'的值对'SelectedIndex'无效)。
如果我禁用系统异常中的调试中断,则prog公羊继续和项目被选中,但我不明白为什么我得到这个错误,如果列表框实际上有项目。
谢谢你的回答,但我认为我的问题在这里有点不同:按照你的建议,我做了一些进一步的调试这就是我得到的:如果我添加了像所描述的新项目,并且我不以编程方式选择它,那么每当我选择它或离开它(在运行时),我总会得到该错误,就像该项目实际上不会不存在。 – 2014-09-12 08:03:28
如果使用“SelectedItem”而不是“SelectedIndex”会发生什么? – Zache 2014-09-15 08:18:59