我想更新列表框中项目的价格 [英] I want to update price of an item in listbox
问题描述
大家好!我正在我的c#应用程序中处理实体框架数据库,但有些事情是如此困难,如我点击图像然后产品名称和产品价格添加到列表框但但当我再次添加相同的项目所以项目将添加没有问题它的价格不是更新相同的产品名称
这是代码:
Hello Guys! i am working on entity framework database in my c# application but some thing here is so difficult like i click on image then product name and product price add in listbox but when i again add same item so item will add no issue with it price is not update in same product name
Here is the code:
Button b = (Button)sender;
tblProduct tp = (tblProduct)b.Tag;
string product = tp.productName;
lbProductsToBuy.Text = tp.ToString();
if (lbProductsToBuy.Items.Contains(tp))
{
MessageBox.Show("Items is already in listbox", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
lbProductsToBuy.Items.Add(tp.productPrice);
}
else
{
}
{
products.Add(tp);
total += (decimal)tp.productPrice;
我尝试过:
按钮b =(按钮)发件人;
tblProduct tp =(tblProduct )b.Tag;
string product = tp.productName;
lbProductsToBuy.Text = tp.ToString();
if(lbProductsToBuy。 Items.Contains(tp))
{
MessageBox.Show(Items已经在列表框中,Duplicate,MessageBoxButtons.OK,MessageBoxIcon.Stop);
返回;
lbProductsToBuy.Items.Add(tp.productPrice);
}
其他
{
}
{
products.Add(tp);
总计+ =(十进制)tp.productPrice;
What I have tried:
Button b = (Button)sender;
tblProduct tp = (tblProduct)b.Tag;
string product = tp.productName;
lbProductsToBuy.Text = tp.ToString();
if (lbProductsToBuy.Items.Contains(tp))
{
MessageBox.Show("Items is already in listbox", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
lbProductsToBuy.Items.Add(tp.productPrice);
}
else
{
}
{
products.Add(tp);
total += (decimal)tp.productPrice;
推荐答案
您正在插入另一个,而不是检查它是否已存在。
You are inserting a other one than you check if it already exists.
lbProductsToBuy.Items.Add(tp.productPrice); // Here you are inserting the price
if (lbProductsToBuy.Items.Contains(tp)) // There you are checking for item and noth its price
if (lbProductsToBuy.Items.Contains(tp.productPrice)) //Solution check if contains price
这篇关于我想更新列表框中项目的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!