问题描述
Hello Guys正在处理一个项目,我正面临一个问题,如果我在列表框中添加项目,所以项目添加成功,现在我想如果我添加相同的项目两次,所以它不添加2次它添加1次,但它显示数量与多重复制金额。
i使用此代码在列表框中添加项目
Hello Guys am working on a project an i am facing a problem if i am adding a item in listbox so items add successfully and now i want if i add same items two times so its not add 2 times it's add 1 times but it's show quantity with mulitplication amount.
i am using this code for adding items in listbox
Button b = (Button)sender;
tblProduct tp2 = (tblProduct)b.Tag;
string product2 = tp2.productName;
listBox2.Text = tp2.ToString();
products2.Add(tp2);
total2 += (decimal)tp2.productPrice;
请看这个图片:
[]
我尝试过:
int Qlty = +1;
string ProductName =((tblProduct)e.ListItem).productName;
string Price =Rs:+ String.Format({0:},((tblProduct)e.ListItem).productPrice) ;
string ProductNamePadded = ProductName.PadRight(33);
e.Value = ProductNamePadded + Qlty + Price;
Please look at this image:
Imgur: The most awesome images on the Internet[^]
What I have tried:
int Qlty = +1;
string ProductName = ((tblProduct)e.ListItem).productName;
string Price = "Rs: " + String.Format("{0:}", ((tblProduct)e.ListItem).productPrice);
string ProductNamePadded = ProductName.PadRight(33);
e.Value = ProductNamePadded+Qlty+Price;
推荐答案
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public void Main()
{
// Simulate order
Order order = new Order
{
TableNumber = "Table 1",
Products = new List<Product>
{
new Product { Name = "Item 1", Price = 100.00m, Quantity = 3},
new Product { Name = "Item 2", Price = 89.00m, Quantity = 2},
new Product { Name = "Item 3", Price = 50.00m, Quantity = 1 }
}
};
foreach(var p in order.Products)
{
Console.WriteLine(p.Name + " (Each: " + p.Price + ")" + " - Qty: " + p.Quantity + " (" + p.Total() + ")");
}
}
}
public class Order
{
public List<Product> Products { get; set; }
public string TableNumber{get;set;}
public decimal Total()
{
if (Products.Any())
return Products.Sum(x => x.Price);
return 0.00m;
}
public Order()
{
Products = new List<Product>();
}
public void RemoveProduct()
{
// Implement here remove logic
}
public bool IsAdded(string productName)
{
return Products.Any(x=>x.Name.ToLower() == productName.ToLower());
}
}
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public decimal Total()
{
return Price * Quantity;
}
}
这篇关于如何更新列表框中的实体框架行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!