【发布时间】:2018-07-18 22:48:40
【问题描述】:
我正在尝试获取搜索到的库存并单击按钮,将其添加到我的订单中的列表框中,以取消下面的代码。 Listbox 项目是一个名为 OrderItems 的对象列表。获取搜索结果后,我检查该项目是否存在于 ListBox 中,只需添加现有项目的 Quantity 属性,否则添加为新项目。逻辑工作正常,它正在循环并识别现有项目,但是 ListBox 中项目的 Quantity 文本不会更改并且始终显示 1。 我正在使用
listBoxItems.DisplayMember = "ItemTitleWithQtyPrice";
listBoxItems.ValueMember = "ItemInventoryId";
还有代码
private void AddItemsToListBox(OrderItem item)
{
Boolean itemExists = false;
listBoxItems.DisplayMember = "ItemTitleWithQtyPrice";
listBoxItems.ValueMember = "ItemInventoryId";
if (listBoxItems != null && listBoxItems.Items.Count > 0)
{
if (listBoxItems.ValueMember != null)
{
foreach (OrderItem it in listBoxItems.Items)
{
if(it.ItemInventoryId == item.ItemInventoryId)
{
it.Quantity = it.Quantity + 1;
it.CGSTAmount = it.CGSTAmount * it.Quantity;
it.SGSTAmount = it.CGSTAmount * it.Quantity;
if (it.DiscountAmount > 0)
{
it.DiscountAmount = it.DiscountAmount * it.Quantity;
}
it.Total = it.GrossPricePerUnit * it.Quantity;
item.ItemTitleWithQtyPrice = item.ItemTitle + " Quantity " + item.Quantity + " Total Price: ₹ " + item.Total;
listBoxItems.Update();
itemExists = true;
GrandTotal = GrandTotal + it.Total;
TotalDiscount = TotalDiscount + it.DiscountAmount;
TotalTaxAmount = TotalTaxAmount + it.GGSTAmount;
order.GrandTotal = GrandTotal;
order.TotalDiscount = TotalDiscount;
order.TaxAmount = TotalTaxAmount;
}
}
if (!itemExists)
{
item.Quantity = 1;
item.Total = item.GrossPricePerUnit * item.Quantity;
item.DiscountAmount = item.DiscountAmount * item.Quantity;
item.GGSTAmount = (item.SGSTAmount + item.CGSTAmount) * item.Quantity;
item.ItemTitleWithQtyPrice = item.ItemTitle + " Quantity " + item.Quantity + " Total Price: ₹ " + item.Total;
listBoxItems.Items.Add(item);
GrandTotal = GrandTotal + item.Total;
TotalDiscount = TotalDiscount + item.DiscountAmount;
TotalTaxAmount = TotalTaxAmount + item.GGSTAmount;
order.GrandTotal = GrandTotal;
order.TotalDiscount = TotalDiscount;
order.TaxAmount = TotalTaxAmount;
}
}
}
else
{
item.Quantity = 1;
item.Total = item.GrossPricePerUnit * item.Quantity;
item.DiscountAmount = item.DiscountAmount * item.Quantity;
item.GGSTAmount = (item.SGSTAmount + item.CGSTAmount) * item.Quantity;
item.ItemTitleWithQtyPrice = item.ItemTitle + " Quantity " + item.Quantity + " Total Price: ₹ " + item.Total;
listBoxItems.Items.Add(item);
GrandTotal = GrandTotal + item.Total;
TotalDiscount = TotalDiscount + item.DiscountAmount;
TotalTaxAmount = TotalTaxAmount + item.GGSTAmount;
order.GrandTotal = GrandTotal;
order.TotalDiscount = TotalDiscount;
order.TaxAmount = TotalTaxAmount;
}
txtTotalDiscounts.Text = order.TotalDiscount.ToString();
txtGrandTotal.Text = order.GrandTotal.ToString();
//// BIND TO DATA SOURCE DATAGRIDVIEW
//var bindingList = new BindingList<OrderItem>(items);
//var source = new BindingSource(bindingList, null);
//dGridViewOrderedItems.DataSource = source;
}
【问题讨论】:
标签: c# winforms listbox listboxitem