0

リスト データ ソースにバインドする ComboBox があります。リストは空で始まり、後で項目を追加したいと思います。問題は、最初の項目を追加するときに ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex' を取得することです。誰かが回避策を持っていますか?

これとまったく同じ問題がここで説明されていますが、解決されたかどうかはわかりません。

コンボボックス--bindingsource-possible-bug

その投稿のコードは次のとおりです。

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#
4

2 に答える 2

0

このように ArraList.Adapter を使用できます-

winforms アプリでコンボボックス コントロールをドラッグし、次のコードを含めます。

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

    items.Add("TestSample1");

ここでは、後で項目を追加しています。

于 2012-06-05T21:18:19.667 に答える
0

ComboBox がバインドされると、データ ソースにない値を追加できなくなります。新しい項目をデータ ソースに追加し、毎回 ComboBox を再バインドすることをお勧めします。

于 2012-05-25T00:50:08.443 に答える