Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody, please assist me if how to validate if the item you want to add in the cart is already exists in listView items? for example I already add CH001 which is the productId of Chocolate strawberry, then I accidentally add again that product Id and what I want is that it should not be acccepted on my listview anymore because CH001 is already on my cart, I search many times about it, its been 1 week from now but I am not able to find how is it to be done, by the way I am adding items to my listview by textbox which auto-populates from my database, and below is my code for adding items in listview which is working properly but no validation.

C#
private void cmdCart_Click(object sender, EventArgs e)
{
listView1.View = View.Details;
 
ListViewItem item2 = new ListViewItem(txtProductName.Text);
item2.Checked = true;
item2.SubItems.Add(txtProductPrice.Text);
item2.SubItems.Add(txtProductID.Text);
 
this.listView1.Items.AddRange(new ListViewItem[] { item2 });
}

I am using visual studio 2010 and I am barely new to c#, so sorry if I am noob, and so much thanks in advance. God Bless to all the helpful people, who are there to help such a noob like me :)

I've tried this one
C#
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!");
}


but no luck at all, probably you can explain me a little bit more. Thanks again.
Posted
Updated 17-Nov-19 4:15am
v2
Comments
[no name] 30-Apr-12 13:33pm    
What are you using as the key?

 
Share this answer
 
v2
Comments
Mohd Imran Saifi 30-Apr-12 10:54am    
Do You use online payment. if yes then can you tell me how to do it.
this is how to check
If ListViewEx1.Items.ToString.Contains("ss") = True Then
MsgBox("yes")
Else
MsgBox("no")
End If
 
Share this answer
 
if(listviewDATA.FindItemWithText(txtName.Text) != null)
{
   MessageBox.Show("Found!");
}
else
{
   MessageBox.Show("Data doesn't Exist");
}
 
Share this answer
 
v2

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