BindingList集合Remove(T)的时候, 出现提供的行索引超出范围异常

jobscq 2014-07-08 10:53:52
开发环境: VS2010, .NET4.0

为了通知datagridview , 我用了BindingList<T>集合作为数据源,发现BindingList<T>只能通知集合数量的增减,
不能更新每行数据的变化,所以有如下的类型:

public class City:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public int ID{get;set;}

private string _address = string.Empty;
public string Address
{
get
{
return _address;
}
set
{
if (value != this._address)
{
_address = value;
NotifyPropertyChanged("Address");
}
}
}

public override bool Equals(object obj)
{
if ((obj as City).ID.Equals(this.ID))
return true;
return false;
}
public override int GetHashCode()
{
return (typeof(City).ToString() + this.ID).GetHashCode();
}
}


为了行数据的实时更新,继承了INotifyPropertyChanged接口,还重写了比较的方法,
然后有对集合的操作


private BindingList<City> cityList = new BindingList<City>();

public void Add(Country c)
{
//这里将Country 转换为 City

cityList.Add(city);
}

public void Remove(Country c)
{
City city = cityList.Where((_c)=> _c.ID == c.ID ).First();
cityList.Remove(city);
}


Add和Remove方法都被外部UI调用

现在问题是这样:当集合的数量是大于2时, 我反复操作UI让集合不停添加删除,偶尔会出现
“提供的行索引超出范围” 的异常, 这里的代码类和方法我都做了缩减,应该就是City类的Equals方法导致的,
这个问题我可以用其他方法解决,但我不知道这里出现异常的原因,
请教各位给个答案!!
...全文
332 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jobscq 2014-07-08
  • 打赏
  • 举报
回复
在补充下错误异常日志:

System.ArgumentOutOfRangeException: 提供的行索引超出范围。
参数名: rowIndex
   在 System.Windows.Forms.DataGridViewRowCollection.GetRowState(Int32 rowIndex)
   在 System.Windows.Forms.DataGridView.OnRowGlobalAutoSize(Int32 rowIndex)
   在 System.Windows.Forms.DataGridView.OnRowHeightChanged(DataGridViewRowEventArgs e)
   在 System.Windows.Forms.DataGridView.OnBandThicknessChanged(DataGridViewBand dataGridViewBand)
   在 System.Windows.Forms.DataGridViewBand.set_ThicknessInternal(Int32 value)
   在 System.Windows.Forms.DataGridView.OnRemovedRow_PostNotification(DataGridViewRow dataGridViewRow, Point newCurrentCell)
   在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
   在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
   在 System.Windows.Forms.DataGridViewRowCollection.RemoveAtInternal(Int32 index, Boolean force)
   在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)
   在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)
   在 System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)
   在 System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
   在 System.ComponentModel.BindingList`1.OnListChanged(ListChangedEventArgs e)
   在 System.ComponentModel.BindingList`1.FireListChanged(ListChangedType type, Int32 index)
   在 System.ComponentModel.BindingList`1.RemoveItem(Int32 index)
   在 System.Collections.ObjectModel.Collection`1.Remove(T item)
jobscq 2014-07-08
  • 打赏
  • 举报
回复
引用 2 楼 mary_zhouk 的回复:
感觉 你应该对obj 做个Null的判断
null判断必须做了的,这里是精简的代码,想用最少的内容说明问题而已,这里的问题真心和对象是否null无关
jobscq 2014-07-08
  • 打赏
  • 举报
回复
引用 1 楼 hefeng_aspnet 的回复:
添加应该不会 应该是你在移除的时候错了 尽量添加时候移除或者移除最后一项没有值
移除的时候错误是肯定的,但移除不可能肯定是移除最后的一项,我这里用了BindingList<T>集合就是为了实时更新, 更不可能在添加的时候才移除啦
江湖的传说 2014-07-08
  • 打赏
  • 举报
回复
感觉应该是数据改动后没有刷新的问题 当bindinglist中的值改变时触发这一事件,事件中写上 dataGridView.RefreshEdit()刷新某单元格 或者 dataGridView.Refresh()全表刷新 就不会出现数据改动滞后的现象。
mary_zhouk 2014-07-08
  • 打赏
  • 举报
回复
感觉 你应该对obj 做个Null的判断
csdn_aspnet 2014-07-08
  • 打赏
  • 举报
回复
添加应该不会 应该是你在移除的时候错了 尽量添加时候移除或者移除最后一项没有值

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧