验证项目是否已存在 [英] validate if item is already exists

查看:68
本文介绍了验证项目是否已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,如果要验证您要在购物车中添加的商品是否已存在于listView商品中,请帮助我?例如我已经添加CH001,这是巧克力草莓的productId,然后我不小心再次添加该产品ID和我想要的是它不应该在我的列表视图上被接受因为CH001已经在我的购物车上,我搜索了很多次关于它,从现在开始已经过了一个星期,但是我无法找到它是如何完成的,顺便说一下,我通过自动填充数据库的文本框向我的列表视图中添加项目,下面是我添加项目的代码在listview中工作正常但没有验证。



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.

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 });
}



我使用的是visual studio 2010,我对c#几乎不熟悉,如果我是菜鸟,那就太抱歉了,非常感谢提前。上帝保佑所有乐于助人的人,他们在那里帮助像我这样的菜鸟:)



我试过这一个


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

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.

推荐答案

你可以使用Contains查看它是否已经存在



You can use Contains to see if it's already there

listView1.Items.Contains





http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.contains.aspx [ ^ ]





http://www.c-sharpcorner.com/UploadFile/mgold/listviewduplicate05082007222909PM/listviewduplicate.aspx [ ^ ]


这是如何检查

如果ListViewEx1.Items.ToString.Contains(ss)= True那么

MsgBox(是)

否则

MsgBox(否)

结束如果
this is how to check
If ListViewEx1.Items.ToString.Contains("ss") = True Then
MsgBox("yes")
Else
MsgBox("no")
End If


if(listviewDATA.FindItemWithText(txtName.Text) != null)
{
   MessageBox.Show("Found!");
}
else
{
   MessageBox.Show("Data doesn't Exist");
}


这篇关于验证项目是否已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆