新手求助!急...帮帮忙

wqpangaishy 2006-08-01 11:14:39
在listview控件里,怎样把选中的行(一行或者多行)删除啊?
谢谢谢!
...全文
95 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingyaohua 2006-09-11
  • 打赏
  • 举报
回复
int Count = ListBox1.Items.Count;
int Index = 0;
for(int i=0;i<Count-1;i++)
{
ListItem Item = ListBox1.Items[Index];
if(ListBox1.Items[Index].Selected == true)
{
ListBox1.Items.Remove(Item); Index--;
}
Index++;
}
diandian82 2006-08-01
  • 打赏
  • 举报
回复
一楼的效率太低了。每必要。
geoffe 2006-08-01
  • 打赏
  • 举报
回复
for ( int i = 0; i <listView1.SelectedIndices.Count; i++ )
{
listView1.SelectedItems[i].Remove();
}
MyLf 2006-08-01
  • 打赏
  • 举报
回复
foreach(ListViewItem item in lstV.SelectedItems)
{
//item.Remove(); 方法一
//lstV.Items.Remove(item); //方法二
}
其中lstV就是一ListView控件
laladeng 2006-08-01
  • 打赏
  • 举报
回复
for ( int i = 0; i < this.listView1.Items.Count; i++ )
{
if (this.listView1.Items[i].Selected == true)
this.listView1.Items[i].Remove();
}
Knight94 2006-08-01
  • 打赏
  • 举报
回复
上面的方法都不完全正确,

删除一行或者多行,首先不能通过foreach,因为删除操作破坏了collection。

用for可以完成,但是多行选中的时候,不能通过正序删除,要反序删除。

所以正确的做法应该如下:
for ( int i = listView1.SelectedIndices.Count - 1; i >= 0; i-- )
{
listView1.Items.RemoveAt( listView1.SelectedIndices[i] );
}

110,531

社区成员

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

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

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