C# 正在尝试从listview中删除行
我的代码根据文本框搜索条件正确标识listview行。但是,当我尝试删除不需要的行时,会收到一条错误消息:-C# 正在尝试从listview中删除行,c#,listview,C#,Listview,我的代码根据文本框搜索条件正确标识listview行。但是,当我尝试删除不需要的行时,会收到一条错误消息:- InvalidArgument=值'-1'对'index'无效 如果有人能帮我解决这个问题,我将不胜感激。。提前谢谢 foreach (ListViewItem item in listView1.Items){ foreach (ListViewItem.ListViewSubItem subItem in item.SubItems){ if (s
InvalidArgument=值'-1'对'index'无效 如果有人能帮我解决这个问题,我将不胜感激。。提前谢谢
foreach (ListViewItem item in listView1.Items){
foreach (ListViewItem.ListViewSubItem subItem in item.SubItems){
if (subItem.Text.ToLower().StartsWith(textBox1.Text.ToLower())){
var index = item.Index;
int.TryParse(listView1.Items[index].SubItems[4].Text, out val);
store[pos] = val;
pos++;
count++;
}else{
listView1.Items[index].Remove();
}
}
}
问题是您没有在“else”子句中计算索引
}else{
listView1.Items[index].Remove();
}
您无法从正在Foreach中迭代的集合中删除项,c#将抛出一个错误,因为集合已更改,请改为使用,并从头到尾执行此操作(因此在删除时不会跳转项)尝试以下方法: 注释在代码中。请随时询问您是否需要其他解释 带Linq的解决方案
using System.Linq;
...
//cast ListViewItemCollection as List<ListViewItem> for easier manipulation
var items = listView1.Items.Cast<ListViewItem>();
//first, get all items that don't have text matching with textBox text
List<int> itemIndexes =
items
.Where(item => !item.Text.ToLower().StartsWith(textBox1.Text.ToLower()))
.Select(item => item.Index).ToList();
int val;
//now, with all those indexes do your stuff
itemIndexes.ForEach(index =>
{
int.TryParse(listView1.Items[index].SubItems[4].Text, out val);
store[pos] = val;
pos++;
count++;
});
//lastly, sort indexes descending (for example: 10, 5,4,1) and remove them from listview
itemIndexes
.OrderByDescending(i=>i)
.ToList()
.ForEach(index => listView1.Items.RemoveAt(index));
//list to hold intem indexes
List<int> itemIndexes = new List<int>();
foreach (ListViewItem item in listView1.Items)
{
foreach (ListViewItem.ListViewSubItem subItem in item.SubItems)
{
var index = item.Index;
if (subItem.Text.ToLower().StartsWith(textBox1.Text.ToLower()))
{
//your code (which doesnt compile since you didn't paste whole code)
//to test this, I had to remove following 4 lines.
int.TryParse(listView1.Items[index].SubItems[4].Text, out val);
store[pos] = val;
pos++;
count++;
}
else
{
itemIndexes.Add(index);
}
}
}
//lastly, sort indexes descending (for example: 10, 5,4,1)
//because if you remove item 1, whole list will shorten and
//you'll get index out of bounds error (in last step when
//trying to remove item with 10 but last index is 7)...
itemIndexes.Reverse();
//... and remove them from listview
foreach (int index in itemIndexes)
{
listView1.Items.RemoveAt(index);
}
使用样本数据进行测试-代码将删除不以a
开头的项目,如bbb
和dd
)
哪一行代码引发该异常?在该类/函数中是否有名为
index
的其他变量?感谢您的早期回复。。。这是给我问题的那一行,不,没有其他名称索引的变量。。。。。。。。。。。。。。。。。。。。listView1.Items[index].Remove();很抱歉我可能误导了你,代码是可以编译的。此错误是运行时错误抱歉,请忽略前面的注释。。。我可能误导了您,这是错误消息,我甚至更改了变量名:--CS0103名称“index”在当前上下文中不存在,'-1'的值对“index”无效。。。此消息是一条运行时消息。。现在,当我右键单击索引时,出现一个消息框,无法导航到carnet下的符号。感谢您的回复。。。您是否可以对您的评论进行扩展或详细阐述…您的评论听起来非常有趣。。。你能不能再详细说明一下,或者告诉我怎么解决?Thanks@BOB.G,您的索引未在任何位置初始化。例如,如果循环的第一步是“else”构造,则编译器尚未初始化索引。非常感谢您的早期响应。我必须承认,我不是这个系统的粉丝,也从来没有使用过这个系统。尽管如此,我将把这件事放在次要位置,等待其他结果。我希望你不介意。再次感谢@BOB.G LOL:)开始使用Linq会很好。毕竟,在引擎盖下,这只是一个很好的老迭代。无论如何,代码中的主要内容不是删除foreach循环中的项;您不能这样做,因为删除更改集合将导致异常。向一些列表中添加索引,然后将其删除,就像我在上一步中所做的那样。如果没有你更喜欢的解决方案,我会为每个版本给你写信干杯,有道理!你能帮我写这个方法吗?我现在将开始拆除else部分。。
ListView listView1 = new ListView();
listView1.Items.Add("aaa");
listView1.Items.Add("bbb");
listView1.Items.Add("ax");
listView1.Items.Add("dd");
TextBox textBox1 = new TextBox();
textBox1.Text = "a";