2012-05-25 2 views
0

목록 데이터 소스에 바인딩하는 ComboBox가 있습니다. 목록은 비어있는 상태에서 시작하여 나중에 항목을 추가하려고합니다. 문제는 첫 번째 항목을 추가 할 때 ArgumentOutOfRangeException이 발생한다는 것입니다. InvalidArgument = '0'값이 'SelectedIndex'에 유효하지 않습니다. 누구든지 해결할 수 있습니까?빈 목록을 ComboBox에 바인딩하고 나중에 항목을 추가하는 방법은 무엇입니까?

정확히 똑같은 문제가 여기에 설명되어 있지만 해결되었는지 확실하지 않습니다.

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); 

그리고 그 결과 스택 추적 : 여기

combobox--bindingsource-possible-bug

은 해당 게시물에서 코드

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# 
+0

나는 WPF와 함께 작업하지만, 나는 ObservableCollection에 바인드하고 비어 나중에 항목을 추가한다. – Paparazzi

+0

@Blam 만 WPF를 사용하고 있었다면 – Yatrix

답변

0

콤보가 결합되면, 어떤을 추가 할 수 없습니다 값이 데이터 소스에 없습니다. 새 항목을 데이터 소스에 추가하고 매번 ComboBox를 다시 바인딩하는 것이 좋습니다. 여기

var items = ArrayList.Adapter(comboBox1.Items); 

    items.Add("TestSample1"); 

, 당신은 나중에 항목을 추가 : 윈폼 응용 프로그램에

드래그 콤보 컨트롤을이 코드를 포함 -

0

는이 같은 ArraList.Adapter 사용할 수 있습니다.

관련 문제