How do I Check for Duplicate Items in a ListView?
2011-07-31 12:01
585 查看
This How To was written in response to the following question in the C# Corner Forums about ListViews:
The user states:
Question: "I have code which I used to check duplication in a ListView, but it doesn't seem to be working. Here is my situation. I have three columns in a ListView and I want to check to make sure that I don't duplicate a field when I add an item to the ListView. How do I do that?"
I tried this code, but it doesn't seem to work:
if (listView1.Items.Contains(lvi) == false)
{
//Add the item to the ListView Control
listView1.Items.Add(lvi);
}
else
{
//Warn user of duplicate entry...
MessageBox.Show("Duplicate Item!");
}
Answer:
This will not work because you are probably not passing the exact same object, but an object that contains the same text. Is this the situation?
For example this works fine with your code:
ListViewItem lvi = new ListViewItem("dog");
Add(lvi);
Add(lvi);
The second time you try to add the same object, you get the message box.
If you want to check the internal information in the row against your Add, you can provide a key in your item. The key corresponds to the name of the item and can be used to compare items using the ContainsKey method:
if (!listView1.Items.ContainsKey(lvi.Name))
{
//Add the item to the ListView Control
listView1.Items.Add(lvi);
}
else
{
//Warn user of duplicate entry...
MessageBox.Show("Duplicate Item!");
}
This will work for the following code with a unique name for your ListViewItem (the unique name being "item1"):
ListViewItem lvi1 = new ListViewItem("dog");
lvi1.Name = "item1";
Add(lvi1);
Otherwise, if you don't provide a key, you'll need to compare the list of items and check each subitem within each item:
private bool IsInCollection(ListViewItem lvi)
{
foreach (ListViewItem item in listView1.Items)
{
bool subItemEqualFlag = true;
for (int i = 0; i < item.SubItems.Count; i++)
{
string sub1 = item.SubItems[i].Text;
string sub2 = lvi.SubItems[i].Text;
if (sub1 != sub2)
{
subItemEqualFlag = false;
}
}
if (subItemEqualFlag)
return true;
}
return false;
}
The user states:
Question: "I have code which I used to check duplication in a ListView, but it doesn't seem to be working. Here is my situation. I have three columns in a ListView and I want to check to make sure that I don't duplicate a field when I add an item to the ListView. How do I do that?"
I tried this code, but it doesn't seem to work:
if (listView1.Items.Contains(lvi) == false)
{
//Add the item to the ListView Control
listView1.Items.Add(lvi);
}
else
{
//Warn user of duplicate entry...
MessageBox.Show("Duplicate Item!");
}
Answer:
This will not work because you are probably not passing the exact same object, but an object that contains the same text. Is this the situation?
For example this works fine with your code:
ListViewItem lvi = new ListViewItem("dog");
Add(lvi);
Add(lvi);
The second time you try to add the same object, you get the message box.
If you want to check the internal information in the row against your Add, you can provide a key in your item. The key corresponds to the name of the item and can be used to compare items using the ContainsKey method:
if (!listView1.Items.ContainsKey(lvi.Name))
{
//Add the item to the ListView Control
listView1.Items.Add(lvi);
}
else
{
//Warn user of duplicate entry...
MessageBox.Show("Duplicate Item!");
}
This will work for the following code with a unique name for your ListViewItem (the unique name being "item1"):
ListViewItem lvi1 = new ListViewItem("dog");
lvi1.Name = "item1";
Add(lvi1);
Otherwise, if you don't provide a key, you'll need to compare the list of items and check each subitem within each item:
private bool IsInCollection(ListViewItem lvi)
{
foreach (ListViewItem item in listView1.Items)
{
bool subItemEqualFlag = true;
for (int i = 0; i < item.SubItems.Count; i++)
{
string sub1 = item.SubItems[i].Text;
string sub2 = lvi.SubItems[i].Text;
if (sub1 != sub2)
{
subItemEqualFlag = false;
}
}
if (subItemEqualFlag)
return true;
}
return false;
}
相关文章推荐
- How do you check if a variable is an array in JavaScript? [duplicate]
- How to implement check box in listView Android to delete listItems
- How-to: disable the web-security-check in Chrome for Mac
- How do you remove the duplicate characters in a given string without using any additional buffer.
- How do you calculate log base 2 in Java for integers?
- JS:Trim() in javascript, how to define a function of checkinput for a WebControl(ascx)
- How to check for and disable Java in OS X
- For loops in R: how to do a “continue”
- How do I set the default schema for a user in MySQL
- How do I add capabilities to my executable in qt for symbian?
- How to Check for Newer Versions of Stored Pages in a Registry
- HowTo: ListView, Adapter, getView and different list items’ layouts in one ListView
- How do I model wind farm for load flow analysis in transmission systems?
- How do I create zip file in Servlet for download?
- How to check for NAN / INF / IND in C++
- How to Find Bapi for Particular Transaction in SAP
- How to Monitor Your Network Usage in Windows 8 (And Prevent Paying For The Extra Bandwidth)
- How to Use the TimeDistributed Layer for Long Short-Term Memory Networks in Python 译文
- How do I convert an enum to a list in C#?
- 如何使用th:each属性迭代模板-原标题:How To Use th:each For Iteration In Thymeleaf Template?