Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I got 3 listViews 2 textbox and 2 buttons in WinForm.

Program Description: The program adds numbers to the listview by typing in numbers in the textbox and clicking the add button

Goal: I want to be able to use the IEnumerable.Except method to output only the unique numbers in listView3, for example in the picture below the unique numbers are 3 and 7 in listView1 and listView2.

C#
 ListViewItem lvi = new ListViewItem(textBox1.Text);
 listView1.Items.Add(lvi);

 ListViewItem lv = new ListViewItem(textBox2.Text);
 listView2.Items.Add(lv);

 //im doing somthing wrong here...
 var nonintersect = listView1.Except(listView2).Union(listView2.Except(listView1));

//populate listview3 with the unique numbers...
// foreach (item )
// {
// }



Error Message: System.Windows.Forms.ListView' does not contain a definition for 'Except' and no extension method 'Except' accepting a first argument of type 'System.Windows.Forms.ListView' could be found (are you missing a using directive or an assembly reference?)



Link to picture
https://picasaweb.google.com/102400252126244020492/IEnumerable#slideshow/6182752875853650498[^]
Posted
Comments
F-ES Sitecore 14-Aug-15 3:25am    
If you want the unique numbers you're probably better using Distinct.

1 solution

try using listview.Items
C#
var nonintersect = listView1.Items.Except(listView2).Union(listView2.Items.Except(listView1));

adding items to list 3
C#
listView3.Items.AddRange(nonintersect.Select(x=>(ListViewItem)x.Clone()).ToArray());
 
Share this answer
 
v6
Comments
Nav-CProject 14-Aug-15 3:15am    
I just used var nonintersect = listView1.Items.Except(listView2).Union(listView2.Items.Except(listView1)); and I'm still having the same error
Nav-CProject 14-Aug-15 3:16am    
Error here: listView1.Items.Except(listView2)
&
Error here: listView2.Items.Except(listView1)
Nav-CProject 14-Aug-15 3:17am    
Error Message: 'System.Windows.Forms.ListView.ListViewItemCollection' does not contain a definition for 'Except' and no extension method 'Except' accepting a first argument of type 'System.Windows.Forms.ListView.ListViewItemCollection' could be found (are you missing a using directive or an assembly reference?) @DamithSL
Nav-CProject 14-Aug-15 3:22am    
Ahh, i see. Thank you. The code you provided me with does not give me an error. How would i now display the unique numbers in listView3? @DamithSL
Nav-CProject 14-Aug-15 3:35am    
Hi i get this error: An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: Cannot add or insert the item '2' in more than one place. You must first remove it from its current location or clone it.

for: listView3.Items.AddRange(nonintersect.ToArray());

@DamithSL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900