I'm using a listview as a shopping cart. I need to know how to recalculate the total value of the cart when I remove an item.
Here is my code for adding to listview;
private void btnACart_Click(object sender, EventArgs e)
{
int value = 0;
for (int i = 0; i < lvCart.Items.Count; i++)
{
value += int.Parse(lvCart.Items[i].SubItems[1].Text);
}
rtbTcost.Text = value.ToString();
}
Here is my code for removing items:
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int total = 0;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
lvCart.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
rtbTcost.Text = total.ToString();
}
I want to recalculate the total value of items an item is removed. How should I do that?