我的列表视图仅在我第一次运行表单时显示数据(在View.Details中),之后它仍然接收数据,只是不显示数据

时间:2019-04-07 18:09:45

标签: c# winforms listview sqlcommand

因此在某些情况下:

我的主窗体'frmMenu'充当主UI菜单窗体,该窗体具有一个面板,该面板将MDI容器中的其他几个子窗体停靠在其上,并且侧面的按钮可在子窗体之间进行切换。我的主要子窗体'frmEquipment'有一个Listview'lstBasket',它充当购物篮,上面装有客户想要购买的任何东西。

在“ frmEquipment”窗体上,您可以浏览以选择所需的项目,当您选择一个项目时,它将打开一个新窗体“ frmEquipmentWindow”,该窗体显示有关该项目的更多信息以及图片。他们在这里选择所需的物品数量,并在确认后将其发送回列表视图,在列表视图中相应地添加名称,数量和价格。

注意:在添加项目之前,请进行检查以确保该项目尚未在“ lstBasket”中,以防止与数量发生冲突。在“ lstBasket”旁边,有一个标签“ lblPrice”,每当添加或更改新商品时,它都会根据购物篮价格进行更新

代码运行良好,我的功能(用于更改数量,添加新项目,无效项目)都运行顺利。因此,问题不仅仅在于我如何在“ lstBasket”中添加或更改项目。

让我解释出现问题的位置:

如果我更改了在'frmMenu'中使用的子窗体,我们先说'frmAdmin',然后再返回'frmEquipment';当我尝试再次添加项目时,所有内容都像正常一样确认,并且'frmEquipmentWindow'关闭(表示已将其添加到'lstBasket'),但'lstBasket'未显示任何数据。

即使没有数据显示,如果我尝试添加相同的项目,我的验证检查也会显示出来,告诉我该项目已经在'lstBasket'中,并且不会让我添加它。但是,“ lblPrice”显然不会更新,而是维持在0.00英镑?

我见过其他人说他们的View出现了问题。详细信息未显示数据,但我在多个帖子中都没有找到解决该问题的方法。我将发布我认为相关的尽可能多的代码,但请询问是否要查看更多代码。

//The code from frmEquipmentWindow that updates the ListView

frmEquipment form = (frmEquipment)Application.OpenForms["frmEquipment"];
                if (form.lstBasket.Items.Count != 0)
                {
                    for (int i = 0; i < form.lstBasket.Items.Count; i++)
                    {
                        string shortName = form.lstBasket.Items[i].SubItems[0].Text;
                        if (shortName == lblBrand.Text + " " + lblName.Tag.ToString())
                        {
                            MessageBox.Show("You already have that item in the basket");
                            basketCheck = true;
                            return;
                        }
                    }
                }
                if (basketCheck == false)
                {
                    DialogResult answer;
                    double totalPrice = itemPrice * temp;
                    answer = MessageBox.Show("The total price is £" + totalPrice.ToString("0.00") + " for  [" + temp + "]  " + lblName.Text, "Are you sure you want to add this to your basket?", MessageBoxButtons.OKCancel, MessageBoxIcon.None);
                    if (answer == DialogResult.OK)
                    {
                        form.lstBasket.Items.Add(lblBrand.Text + " " + lblName.Tag.ToString());
                        form.lstBasket.Items[form.lstBasket.Items.Count - 1].SubItems.Add(temp.ToString());
                        if (priceCheck)
                        {
                            form.lstBasket.Items[form.lstBasket.Items.Count - 1].SubItems.Add("£" + totalPrice.ToString());
                        }
                        else
                        {
                            form.lstBasket.Items[form.lstBasket.Items.Count - 1].SubItems.Add("£" + totalPrice.ToString() + ".00");
                        }
                        form.lstBasket.View = View.Details;
                        form.updateBasketPrice();
                        GlobalVariables.itemsInBasket = true;
                        this.Close();
                        return;
                    }
                    else
                    {
                        return;
                    } 
                }

我没有在代码中声明ListView,但它是在Designer类中为以下形式生成的:

        // 
        // lstBasket
        // 
        this.lstBasket.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.clmN,
        this.clmQ,
        this.clmP});
        this.lstBasket.FullRowSelect = true;
        this.lstBasket.Location = new System.Drawing.Point(739, 117);
        this.lstBasket.MultiSelect = false;
        this.lstBasket.Name = "lstBasket";
        this.lstBasket.Size = new System.Drawing.Size(303, 379);
        this.lstBasket.TabIndex = 5;
        this.lstBasket.UseCompatibleStateImageBehavior = false;
        this.lstBasket.View = System.Windows.Forms.View.Details;
        this.lstBasket.SelectedIndexChanged += new System.EventHandler(this.lstBasket_SelectedIndexChanged);
        // 
        // clmN
        // 
        this.clmN.Text = "Name";
        this.clmN.Width = 156;
        // 
        // clmQ
        // 
        this.clmQ.Text = "Quantity";
        this.clmQ.Width = 65;
        // 
        // clmP
        // 
        this.clmP.Text = "Price";
        this.clmP.Width = 85;

0 个答案:

没有答案