InvalidArgument =值为'4'对listview选择项中的'index'无效。 [英] InvalidArgument=Value of '4' is not valid for 'index' in listview selecting items.

查看:2082
本文介绍了InvalidArgument =值为'4'对listview选择项中的'index'无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (reader.Read())
{
  listView2.Items.Add(reader["YearLabel"].ToString());
  foreach (ListViewItem item1 in listView1.Items)
  {
    if (reader.HasRows)
    {
        dt.Load(reader);
        sda.Fill(dt);
    }
    int i;
    for (i = 0; i <= dt.Rows.Count - 1; i++)
    {
      listView2.Items.Add(dt.Rows[i].ItemArray[0].ToString());
      if (listView2.SelectedItems.Count > 0)
      {
        for (i = 0; i <= listView2.Items.Count - 1; i++)
        {
            if (listView2.Items[i].Selected == true)
            {
              break;
            }
        }
      }
      //listView2.Items[i].SubItems.Add(dt.Rows[i].ItemArray[1].ToString());
      StringBuilder sb = new StringBuilder();
      for (i = 0; i < listView2.Items.Count ; i++)
      {
        for (int j = 1; j < listView2.Items.Count; j++)
        {
          this.listView2.View = View.Tile;
          if (i == j)
          {
              if (listView2.Items[i].Text != listView2.Items[j].Text)
              {
                  sb.AppendLine(listView1.Items[j].Text);
                  listView2.Items[j].Remove();
              }
          }
        }
      }
//[...]
    }
  }
}

推荐答案

你'重新删除你正在经历的列表中的项目...所以你删除了一个项目,计数不再有效,索引更改,你得到溢出。



正确的实现是将列表计数设置为变量,每次删除项目时,将计数减少一个,这样就不会超过最大长度。



或者,你可以通过listView2添加项目到listView1,然后通过listView1从listView2中删除东西。



如果这有帮助请花时间接受解决方案。谢谢。
You're removing items from the list you're going through...so you remove one item, the count is no longer valid, indexes change and you get an overflow.

Correct implementation is to set list count into a variable and each time you remove an item, decrease the count by one so you'll never exceed maximum length.

Alternatively, you could go through listView2 adding items to listView1 and then go through listView1 deleting things from listView2.

If this helps please take time do accept the solution. Thank you.


这篇关于InvalidArgument =值为'4'对listview选择项中的'index'无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆