2014-11-05 73 views
-1

我想使用BindingList作爲C#中的數據源,但每當我嘗試將第一項添加到BindingList,我得到一個ArgumentOutOfRangeException拋出。 在我的項目中,我需要清除BindingList,並在將其設置爲DataSource後添加新​​項目。ArgumentOutOfRangeException當添加第一項到綁定列表數據源

BindingList<string> dataSource = new BindingList<string>(); 
myComboBox.DataSource = dataSource; 
dataSource.Add("something"); // Exception, here. 

我知道這個異常由.NET Framework處理,但我需要找到一種方法來避免它。

我也試過

List<DirectoryInfo> list = aList.ToList(); 
list.AddRange(bList); 

//set the index to -1 as suggested 
var index = myComboBox.SelectedIndex; 
myComboBox.SelectedIndex = -1; 

myComboBox.DataSource = null; 
bindingList.Clear(); 
bindingList.AddRange(list.Distinct()); 
if (bindingList.Count > 0) 
{ 
    //internal ArgumentOutOfRangeException here when binding 
    myComboBox.DataSource = bindingList; 
    myComboBox.SelectedIndex = index; 
} 
else 
    myComboBox.DataSource = null; 

謝謝你,這裏是調用堆棧

System.Windows.Forms.dll!System.Windows.Forms.ComboBox.SelectedIndex.set(int value) 
System.Windows.Forms.dll!System.Windows.Forms.ListControl.DataManager_PositionChanged(object sender, System.EventArgs e) 
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) 
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) 
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) 
System.dll!System.ComponentModel.BindingList<System.__Canon>.OnListChanged(System.ComponentModel.ListChangedEventArgs e) 
System.dll!System.ComponentModel.BindingList<System.__Canon>.FireListChanged(System.ComponentModel.ListChangedType type, int index) 
System.dll!System.ComponentModel.BindingList<System.__Canon>.InsertItem(int index, System.__Canon item) 
######.dll!System.Collections.ObjectModel.Collection<###########>.Add(######### item) 
+0

因此當您更改順序並首先將數據源分配給BindingList然後綁定到組合框時會發生什麼? – MethodMan 2014-11-05 20:53:16

+0

異常來自組合框。由於它已綁定到數據源BindingList,所以當您添加到BindingList時,組合框會發現它已設置爲不在列表中的值和barfs。因此,首先將所需的一切添加到BindingList,然後將組合框綁定到列表,錯誤消失。 – StarPilot 2014-11-05 21:18:14

+0

問題是:「我需要清除BindingList,並在將其設置爲DataSource後添加新​​項目。」我知道這個異常是由comboBox引發的,我們可以在callstack中看到它。但換行不是我的項目中的可用選項。 – Heyjee 2014-11-05 21:25:11

回答

0

Finaly,我擺脫我的問題!這是我找到的解決方案

首先,我將DataSource設置爲null,因爲當我啓動時BindingList總是空的。

comboBox1.DataSource = null; 

然後,當我需要更新的BindingList並添加一些項目我在這裏做這樣的事情

List<DirectoryInfo> list = aList.ToList(); 
list.AddRange(bList); 

BeginUpdate(); 
bindingList.Clear(); 
bindingList.AddRange(list.Distinct()); 
EndUpdate(); 

是的BeginUpdate和EndUpdate方法

private void BeginUpdate() 
{ 
    //if the DataSource is going to be empty, adding the first item will 
    //always trigger an ArgumentOutOfRangeException on the selected index. 
    //to avoid this, we must stop the binding during the modification of the list. 
    _Updating = true; 
    comboBox1.DataSource = null; 
} 

private void EndUpdate() 
{ 
    if (bindingList.Count > 0) 
    { 
     //the binding will set SelectedIndex to the old index when rebinding and it's possibily the wrong one. 
     //the good index is always 0 but we set the index to -1 before to be sure to be able to trigger the 
     //selectedIndex_changed method after _UpdatingRecentFolders is turn to false 
     comboBox1.DataSource = bindingList; 
     comboBox1.SelectedIndex = -1; 
     _Updating = false; 
     //I always want 0 but you can place the index you want 
     comboBox1.SelectedIndex = 0; 
    } 
    else 
    { 
     comboBox1.DataSource = null; 
     _Updating = false; 
     comboBox1.SelectedIndex = -1; 
    } 
} 

我也有selectedIndex_changed事件

private void comboBoxItem_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (!_Updating && comboBox1.SelectedIndex != -1) 
     aControl.SelectedItem = bindingList[comboBox1.SelectedIndex]; 
} 

我希望這會幫助別人!

1

嘗試這樣的事情在這裏被我只是測試在我的評論說一個例子

var blist = new BindingList<string>(); 
blist.Add("something"); 
blist.Add("something2"); 
comboBox1.DataSource = blist; 

如果您需要首先將其分配空白,請執行如下操作:

if(blist.Count > 0) 
{ 
    comboBox1.DataSource = null; 
    var blist = new BindingList<string>(); 
    blist.Add("something"); 
    blist.Add("something2"); 
    comboBox1.DataSource = blist; 
} 

List<T>.Add方法或BindingList.Add方法 也看.Clear()方法和一些研究。 Count()方法

替代使用的BindingList會的BindingSource在這裏找到更多的閱讀關於這個主題MSDN BindingSource Class

+0

你正在做'if(blist.Count> 0)',並且在創建'var blist = new BindingList ();'這段代碼不會按預期工作! – Heyjee 2014-11-06 14:11:48

相關問題