【问题标题】:Removing item and value in Listview删除 Listview 中的项目和值
【发布时间】:2014-02-16 14:17:19
【问题描述】:

我在从列表视图中删除项目时遇到问题。该程序计算列表中剩余值的总值。问题是当我删除一个项目时,它会删除列表视图中添加的第一个项目的值。

例如:

/*I added this items in order.

item1 = 20,
item2 = 10,
item3 = 5

When I remove item2 its rtbTcost is 15 based on the program below.
Which means that the value of item1 was removed.*/

                int totalRemoved = 0;


                for (int i = 0; i < lvCart.SelectedItems.Count; i++)
                {
                    totalRemoved += int.Parse(lvCart.Items[i].SubItems[1].Text);
                    lvCart.Items.Remove(lvCart.SelectedItems[i]);

                }

                _listTotal -= totalRemoved;
                rtbTcost.Text = _listTotal.ToString();

【问题讨论】:

    标签: c# listview


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      1从结尾开始倒计时。

      for (int i = lvCart.SelectedItems.Count - 1; i >= 0; i--)
      {
              ListViewItem itm = lvCart.SelectedItems[i];
              lvCart.Items[i].Remove();
      }
      

      删除总数为

      lvCart.SelectedItems.Count
      

      如果您正在使用从列表视图中选择的项目,作为要删除的项目。

      删除后求和,通过对列中的值求和。

      int _listTotal = 0;
      foreach (ListViewItem li in lvCart) {
         _listTotal += int.Parse(li.Subitems[1].Text);
      }
      rtbTcost.Text = _listTotal.ToString();
      

      【讨论】:

        猜你喜欢
        • 2016-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多