2012-12-04 52 views

回答

1

您可以撥打ListViewItem上的ListViewItem.Remove方法您想要刪除。

此方法與ListView控件中包含該項目的ListView.ListViewItemCollection的Remove方法在功能上類似。您可以使用Remove方法從其ListView控件中刪除項目。如果要將項目移動到其他ListView控件,或者需要根據用戶的請求刪除項目以從應用程序中刪除項目,則此功能可能很有用。

例如,你可以通過所有的ListViewItems的週期在ListView,看Tag,並Remove要刪除的項目:

// Create the ListView and ListViewItem. 
ListView myList = new ListView(); 
ListViewItem myItem = new ListViewItem { Tag = "MyTag", Text = "My ListViewItem" }; 
myList.Items.Add(item); 

// Look for the ListViewItem with a Tag of "MyTag" and remove it. 
foreach (ListViewItem item in myList.Items) { 
    if (String.CompareOrdinal(item.Tag as string, "MyTag") == 0) { 
     i.Remove(); 
     break; 
    } 
} 

這個例子除去第一匹配後退出項目。如果需要刪除多個匹配的ListViewItems,則需要將這些匹配集合起來,然後再刪除它們。

0

將來您應該向社區展示您爲問題付出的努力量。你可以告訴我們你的嘗試,並給我們一些例子。但是,下面是一些應該幫助解答您的問題的代碼。

int id = 0; 

foreach (ListViewItem item in listView1.Items) 
{ 
    if((int)item.Tag == id) 
    { 
     item.Remove(); 
     break; 
    } 
} 

注意:這裏假定標記是一個整數。如果情況並非總是如此,那麼您應該添加代碼以防止錯誤。

1

這是我做的一個快速功能。它使用linq。 myTag可以是任何對象。

 string myTag = "aaa"; 

     List<ListViewItem> lst = listView1.Items.Cast<ListViewItem>().Where(i => i.Tag == myTag).ToList(); 
     if (lst.Count != 0) 
     { 
      listView1.Items.Remove(lst.First()); 
     }