在ListBox项C#中编辑现有对象的属性

时间:2018-07-18 22:48:40

标签: c# winforms listbox listboxitem

我试图获取搜索到的库存并单击按钮,然后通过取消下面的代码将其添加到订单中的列表框中。列表框项目是称为OrderItems的对象的列表。提取搜索结果后,我检查该项目是否存在于ListBox中,并仅添加现有项目的Quantity属性,否则将其添加为新项目。逻辑工作正常,正在循环并可以识别现有项目,但是ListBox中该项目的Quanity文本不会更改,并且始终显示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;
    }

1 个答案:

答案 0 :(得分:0)

您的代码中可能存在几个问题,但是您需要提供更多详细信息。但是,如果数量未在listBox中显示为正在更新,则请尝试按照您的感觉为您更新的以下代码。未经测试的代码请检查并给出关于它的反馈。

export default typography;

还有另外一件事建议您增加休息时间;点在您的搜索循环检查中,因为您正在搜索一项或使用Linq的概念通过ID查找。