我有一个绑定到列表数据源的组合框。该列表开始为空,稍后我想向其中添加项目。问题是当我添加第一项时,我得到 ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'。有人有工作吗?
此处描述了完全相同的问题,但我不确定它是否已解决。
这是该帖子的代码:
BindingList<int> bl = new BindingList<int>();
BindingSource bs = new BindingSource();
ComboBox cb = new ComboBox();
this.Controls.Add(cb);
cb.DataSource = bs;
bs.DataSource = bl;
//bs.DataError += delegate { throw new Exception("DataError"); };
bl.Add(99);
以及由此产生的堆栈跟踪:
System.Windows.Forms.dll!System.Windows.Forms.ComboBox.SelectedIndex.set(int value) + 0x1e8 bytes
System.Windows.Forms.dll!System.Windows.Forms.ListControl.DataManager_PositionChanged(object sender, System.EventArgs e) + 0x36 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x39 bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0x16a bytes
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x2f9 bytes
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x82 bytes
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.InnerList_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x2e bytes
System.dll!System.ComponentModel.BindingList<int>.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x17 bytes
System.dll!System.ComponentModel.BindingList<int>.InsertItem(int index, int item) + 0x62 bytes
mscorlib.dll!System.Collections.ObjectModel.Collection<int>.Add(int item) + 0x36 bytes
WindowsFormsApplication1.exe!WindowsFormsApplication1.Form1.button1_Click(object sender, System.EventArgs e) Line 35 + 0x10 bytes C#