• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1654
  • Last Modified:

Total in a ListView

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

0
linkcube1
Asked:
linkcube1
  • 3
2 Solutions
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Example:
        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();
                }
                CalculateTotal();
            }
        }

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

0
 
jdavistxCommented:
It looks like you're on the right track, and I can't remember off the top of my head, but I believe you're right in that as you iterate over your list, and remove items, your for loop just keeps going but you're getting unexpected results.  I believe that's why you had to use a for loop in your Delete; however, you could use a foreach in your Add.  I think you get would get a runtime error if you were to attempt to delete items from the ListView collection in a foreach.

With that said, yv989c had some good advice -- move your total calculation to a function.  For that matter, you should move all of your logic out of the event handlers and in to their own functions.  You can use nifty LINQ to get the sum of your items with a one-liner:

private void btnAddToReceipt_Click(object sender, EventArgs e)
{
	if (listView1.SelectedItems.Count > 0)
		AddItems();
}
        
private void btnDelete_Click(object sender, EventArgs e)
{
	if (listView2.SelectedItems.Count > 0)
		DeleteItems();
}

private void AddItems()
{
	listView1.BeginUpdate();
	foreach (var item in listView1.SelectedItems)
	{
		ListViewItem newItem = (ListViewItem)item.Clone();
		listView2.Items.Add(newItem);
	}
	listView1.EndUpdate();
	
	UpdateTotal();
}

private void DeleteItems()
{
	listView2.BeginUpdate();
	for (int i = listView2.SelectedItems.Count; i > 0; i--)
		listView2.Items.Remove(listView2.SelectedItems[i - 1]);
	listView2.EndUpdate();

	UpdateTotal();
}

private void UpdateTotal()
{
	var total = (from l in listView1.Items.Cast<ListViewItem>() select ((MemberBook)l.Tag).Price).Sum();
	txtTotal.Text = String.Format("${0:0.00}", total);
}

Open in new window

0
 
linkcube1Author Commented:
ok thanks for the tips guys!
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Glad to help!
and nice advice jdavistx!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now