我尝试在C#中使用BindingList作为DataSource,但每当我尝试将第一个项添加到BindingList时,我都会抛出ArgumentOutOfRangeException。 在我的项目中,我需要清除BindingList并在将新项目设置为DataSource后添加新项目。
BindingList<string> dataSource = new BindingList<string>();
myComboBox.DataSource = dataSource;
dataSource.Add("something"); // Exception, here.
我知道异常是由.Net框架处理的,但我需要找到一种方法来避免它。
我也试过
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;
谢谢,这是callstack
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 :(得分: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
答案 1 :(得分:0)
最后,我摆脱了我的问题!这是我找到的解决方案
首先,我将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];
}
我希望这能帮助别人!