检查列表视图是否包含项目 [英] Check if list view contain item
问题描述
我有一个包含3列(id,name,author)的listview,我使用此方法添加行:
i have a listview with 3 columns (id , name, author), i use this method to add row:
public void addToLv(Book book)
{
//TODO: Verifier si l'item existe avant d'ajouter
ListViewItem lvi1 = new ListViewItem(book.id.ToString());
lvi1.Text = book.id.ToString();
lvi1.SubItems.Add(book.name);
lvi1.SubItems.Add(carte.author);
listView1.Items.Add(lvi1);
}
现在,在插入新书以避免重复的元素之前,我将不检查书是否存在,我尝试使用此代码,但无法正常工作我已经使用了这条线,但是没有用:
Now i wan't to check if book exists before i insert the new one to avoid duplicate element, i try this code but it'S not working i have use this line but it's not working:
(listView1.Items.ContainsKey(book.id))
{
listView1.Items.Add(lvi1);
}
能帮我吗?谢谢
推荐答案
我认为您错过了!"(不是)在您的代码中.
I think you missed a "!" (not) in your code.
(!listView1.Items.ContainsKey(book.id))
{
listView1.Items.Add(lvi1);
}
您的代码说,如果列表视图包含该键,则将添加另一个包含该键的条目.但是似乎您想做相反的事情,对不对?如果您的列表视图不包含带有该键的条目,则您要添加具有该键的条目.
Your code is saying that if your listview contains that key, you will add another entry that has that key. But it seems you want to do the opposite, right? If your listview does not contain an entry with that key, you want to add an entry that has that key.
这篇关于检查列表视图是否包含项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!