如何更新列表框中的实体框架行 [英] How to update entity framework row in listbox

查看:67
本文介绍了如何更新列表框中的实体框架行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;







请看这个图片:

Imgur:互联网上最棒的图片 [ ^ ]



我尝试过:



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;

推荐答案

我会按如下方式设计对象。可以将此解决方案复制到控制台应用程序进行测试。

看到它在这里工作 [ ^ ]



注意我已添加一个存根方法,你可以检查是否已经添加了一个项目,虽然以下逻辑可以帮助你避免手动和繁琐地检查列表中是否有东西。



I would design the objects as follows. This solution can be copied to a console app to test it.
See it working here[^]

Notice I've added a stub method for you to check if an item is already added, although the following logic should help you avoid having to check manually and tediously if something is already in a list.

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


这篇关于如何更新列表框中的实体框架行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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