Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Friends,
This is my Subway Resturant Project, Which is almost complete but I m facing a little problem, I m new in C# so please help me.

I want when I want to add an item in listView2 from listView1 and it already contain that item then item will not add but quantity increase.I use a code below(in Bold) but it's not working for me, so please guide me. Thankyou!

GUI:http://imageshack.us/photo/my-images/717/guiyp.jpg/[^]
C#
if (listView2.Items.ContainsKey(listView1.Name))
                       {
                           MessageBox.Show("Duplicate");
                           //int quantity = 0;
                           //quantity = quantity + int.Parse(dRow.ItemArray.GetValue(3).ToString());
                           //item1.SubItems.Add(quantity.ToString());

                       }
                       else
                       {
                           listView1.Focus();
                           item1.SubItems.Add(dRow.ItemArray.GetValue(2).ToString());
                           listView2.Items.AddRange(new ListViewItem[] { item1/*,item2,item3*/ });
                           item1.SubItems.Add(dRow.ItemArray.GetValue(3).ToString());

                       }
Posted
Updated 5-May-12 19:01pm
v4
Comments
Sergey Alexandrovich Kryukov 6-May-12 1:01am    
Reason for my vote of 3
Pointless long code dump, very bad code, and -- very bad site. Repelling pictures and title not graphically related (inconsistent disgusting shadows and outline), unrelated content, and -- a very bad sin -- a pop up, very annoying. Decent people usually close such pages before they read what is is about, will hardly ever use the service.

If you want so show the code (in most cased you should), create a complete but very short code sample showing only one problem and nothing else.

--SA
Sandeep Mewara 6-May-12 1:01am    
Removed whole code dump and just kept the related code snippet.

1 solution

First of all, don't work with UI, work with data. The UI should get into play when all the data is already prepared. Separate data model from UI.

The ready-to-use data structures preventing duplicates are the following generic types:

System.Collections.Generic.Dictionary<,><br />
System.Collections.Generic.SortedDictionary<,><br />
System.Collections.Generic.SortedList<,>


Please see:
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^],
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^],
http://msdn.microsoft.com/en-us/library/f7fta44c.aspx[^],
http://msdn.microsoft.com/en-us/library/ms132319.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
princeshils 6-May-12 4:05am    
Please give me an example related to my code.
Sergey Alexandrovich Kryukov 7-May-12 12:22pm    
What you should do is pretty far from what you have written. "Related to my code" is not well-defined notion. I suggest you use one of the classes I referenced above to work with data to exclude duplicates and ask a question if you face any problems.
--SA
Espen Harlinn 7-May-12 6:03am    
Good reply :-D
Sergey Alexandrovich Kryukov 7-May-12 12:21pm    
Thank you, Espen.
--SA

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