【发布时间】:2014-09-11 10:37:53
【问题描述】:
我在尝试通过绑定源选择添加到列表框的新项目时遇到此异常。
这是带有自定义对象数据源的 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' 无效。)
如果我在系统异常中禁用调试中断,程序会继续运行并且项目被选中,但如果列表框实际上有项目,我无法理解为什么我会收到该错误。
【问题讨论】:
标签: c# winforms binding listbox