ListView.ListViewItemCollection.Add这个函数调用的时候出错了。
我开了一个线程,每发送一次数据,就在ListView里面显示出来,结果程序运行一段是时间后,就报错,错误如下:System.ArgumentOutOfRangeException:Insertion index was out of range. Must be non-negative and less than or equal to size.
Parameter name:index
  at System.Collections.ArrayList.Insert(Int32 index,Object value)
  at System.Windows.Forms.ListView.InsertItems(Int32 displayIndex,ListViewItem[] items,Boolean checkHosting)
  at System.Windows.Forms.ListView.IistViewNativeItemCollection.Add(ListViewItem value)
  at System.Windows.Forms.ListView.IistViewItemCollection.Add(ListViewItem value)是不是我添加的ListViewItem太多了,超过了允许的最大数量,所以出错了?

解决方案 »

  1.   

    Int32值很大的,你Add超出Int32范围了?
      

  2.   

    是不是Insert后面那个index参数你不小心搞成负的了?
      

  3.   

    我是调用Add这个成员函数的,他弹错的时候提示上面这些。
    Add根本不需要index的,所以应该不会出现负数。
      

  4.   

    看这个提示,貌似调用了insert
      

  5.   

    System.Windows.Forms.ListView.IistViewItemCollection.Add(ListViewItem value)
    我的函数用的是这个。
    是不是他自己内部调用了insert函数?
      

  6.   


    System.Windows.Forms.ListViewItem view1 =new ListViewItem(SomeTypes.displaystring(this.CForm.NO, System.DateTime.Now.ToString(), 0, s));
    this.CForm.NO++;
    this.CForm.ItemAdd(view1);
            public void ItemAdd(ListViewItem view1)
            {
                if (this.listViewBytes.InvokeRequired)
                {
                    SetListViewItem AddItem = new SetListViewItem(ItemAdd);
                    this.listViewBytes.Invoke(AddItem, new object[] { view1 });
                }
                else
                {
                    this.listViewBytes.Items.Add(view1);
                }
            }
      

  7.   

    是不是因为多线程并发操作导致的混乱?加上锁试试
    比如add的时候加上SuspendLayout()和ResumeLayout()
      

  8.   

    这个好像是你线程乱了
    你使用lock一下你的函数
      

  9.   

    ListView提供了BeginUpdate和EndUpdate的方法来解决大数据更新
      

  10.   

    我调用的是Add函数,为什么会出现insert越界呢?是Add里面调用了Insert?
      

  11.   

    我调用的是Add函数,为什么会出现insert越界呢?是Add里面调用了Insert?
    没有人知道这个答案吗?
      

  12.   

    先不说多线程的问题,我现在很奇怪,为什么调用Add会弹出这些异常呢?
    System.ArgumentOutOfRangeException:Insertion index was out of range. Must be non-negative and less than or equal to size.
    Parameter name:index
      at System.Collections.ArrayList.Insert(Int32 index,Object value)
      at System.Windows.Forms.ListView.InsertItems(Int32 displayIndex,ListViewItem[] items,Boolean checkHosting)
      at System.Windows.Forms.ListView.IistViewNativeItemCollection.Add(ListViewItem value)
      at System.Windows.Forms.ListView.IistViewItemCollection.Add(ListViewItem value)难道Add里面有调用Insert??
      

  13.   

    多线程情况下 insert的确会出现问题的
      

  14.   

    ListView确实有一个上限
    我记得以前测试的使用 
    用一个Timer 不断添加数据
    好像是1000吧就奔溃了
      

  15.   

    我猜测ListView.InsertItems调用的时候 Items为空
    以前遇到过类似的问题  ListView 控件用Add函数的时候,如果集为空会自动new一个
    如果用Insert的话,则不会自动new