I have the code which i used to check duplication in listbox but not working? Q. I have 3 columns in listview grid and i want to check duplication with only one field. How to do that? Code: 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!"); } Thanks, Please help
Contains does not work in your situation but you need to be having something to go through the entire list and check.
What would be the best way then? Can you give me the loopong condition here please? e.g. foreach(lvt.SubItems in scheduleListView.Items) //This above give me error "Type and identifier are both required in a foreach statement"
foreach(lvt.SubItems in scheduleListView.Items) you need to take the strings in the items and not the subitems.
You need to get the string of the items and then compare with the one you would like to add new and see if it matches.
This below code doesnt work foreach(string itemtext in scheduleListView.Items) // change it to subitems or as needed?? The main issue is first of all you have to check if the list is null if it is null then just insert the new item when you try to insert item again it will check for duplication. how would you implement that? Just help me small bit in looping condition. Thanks Shabbir
It worked. Thanks added new function to check the duplication Code: private bool IsInCollection(ListViewItem lvi) { foreach (ListViewItem item in scheduleListView.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; }