We help IT Professionals succeed at work.

Total in a ListView

linkcube1
linkcube1 asked
on
1,929 Views
Last Modified: 2012-05-11
In the code below I have a Total variable that calculates a total from items in a listview. My problems is when items are deleted the total isn't calculated properly. It seems the total gets changed during deletion by removing the top item off the listview and I am looking to delete the item of the selected index in the listView. So If i add  3 items of (in price) 2.00 3.00 and 5.00 and delete the 3.00 it should return 7.00 while now it returns 5.00. I am not sure how to correct this so any help would be great thanks.
private void btnAddToReceipt_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                
                foreach (ListViewItem item in listView1.SelectedItems)
                {
                    ListViewItem newItem = (ListViewItem)item.Clone();
                    listView2.Items.Add(newItem);
                    txtTotal.Refresh();
                    currentTotal = ((MemberBook)newItem.Tag).Price;
                }
                total = total + currentTotal;
                txtTotal.Text = "$ "  + total.ToString();
            }
        }
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count > 0)
            {
                for (int i = listView2.SelectedItems.Count; i > 0; i--)
                {
                    listView2.Items.Remove(listView2.SelectedItems[i - 1]);
                    
                    //total = total - currentTotal;
                    //txtTotal.Text = "$ " + total.ToString();
                }
                total = total - currentTotal;
                txtTotal.Text = "$ " + total.ToString();
            }
        }

Open in new window

Comment
Watch Question

Carlos VillegasFull Stack .NET Developer

Commented:
Hi, just use this code when you want to recalculate the total:
private void CalculateTotal()
{
    decimal total = 0;
    foreach (ListViewItem item in listView1.Items)
    {
        total += ((MemberBook)item.Tag).Price;
    }
    txtTotal.Text = total.ToString("C");
}

Open in new window

Full Stack .NET Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
ok thanks for the tips guys!
Carlos VillegasFull Stack .NET Developer

Commented:
Glad to help!
and nice advice jdavistx!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.