【问题标题】:Additional information: InvalidArgument=Value of '0' is not valid for 'index'附加信息:InvalidArgument=“0”值对“索引”无效
【发布时间】:2016-12-06 04:55:21
【问题描述】:

我意识到这可能看起来像一个重复的问题(我已经看到许多其他问题询问此错误),但是我找不到解释我遇到的问题的答案。该错误是由调用引发的

invList.SelectedItems[0].Text //invList is a ListView

从我读到的内容来看,此指令引发的错误表明尝试访​​问 ListView 中选定项目的空列表(或数组?)。但是,我尝试访问的 ListView 在指令执行时填充了项目。以下是我发出此指令的方法:

方法#1:删除项目

public bool deleteItem()
    {
        if (invList.SelectedItems.Count == 0) //if no item selected...
        {
            MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return false;
        }
        else
        {
            DialogResult dialogResult = MessageBox.Show(
                "Are you sure you want to delete item: " + invList.SelectedItems[0].Text + "?",
                "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dialogResult == DialogResult.Yes)
            {
                for (int i = 0; i < itemRecords.Count; i++)
                {
                    if ((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == clinicName))
                    {
                        itemRecords.Remove(itemRecords[i]);
                        listRefresh();
                    }
                }
                return true; //return true to indicate that data has been edited
            }
            return false; // return false to indicate that nothing has changed
        }
    }

方法#2:更新项目

 public bool updateItem()
    {
        if (invList.SelectedItems.Count == 0) //if no item selected
        {
            MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return false;
        }
        else
        {
            for (int i = 0; i < itemRecords.Count; i++)
            {
                //if id == the id of the selected item
                if((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == this.Text))
                {
                    ItemAddition itemAddition = new ItemAddition(itemRecords, itemRecords[i], this);
                    itemAddition.ShowDialog();
                }
            }
            return true;
        }
    }

列表刷新:

       public void listRefresh()
    {
        invList.Items.Clear();
        loadItems();
    }

加载项:

        private void loadItems()
    {
        foreach (Record r in itemRecords)
        {
            if (r.practice == clinicName)
                invList.Items.Add(r.ToString());
        }
    }

调用第一个方法时会调用错误,但调用第二个方法时不会调用该错误。这种不一致是我困惑的原因。这个错误只会出现在第一种方法中是否有某种原因?

【问题讨论】:

  • 可以添加listRefresh方法的代码吗?
  • @Damith 当然,我已经添加了。
  • 尝试修改for (int i = 0; i &lt; itemRecords.Count-1; i++)

标签: c# listview selecteditem


【解决方案1】:

删除项目后的移动刷新方法。如果您移动项目并重新绑定,那么您将失去选择。

   for (int i = 0; i < itemRecords.Count; i++)
    {
        if ((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == clinicName))
        {
            itemRecords.Remove(itemRecords[i]);
            //listRefresh();
        }
    }

listRefresh();                
return true;

【讨论】:

  • 当我看到你的回答时,我刚刚找到了一个粗略的解决方案。这正是问题所在。非常感谢您的帮助。
猜你喜欢
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多