?
Solved

Total in a ListView

Posted on 2011-04-25
5
Medium Priority
?
1,586 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

0
Comment
Question by:linkcube1
  • 3
5 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35464574
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
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 1000 total points
ID: 35464578
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
 
LVL 7

Assisted Solution

by:jdavistx
jdavistx earned 1000 total points
ID: 35466906
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
 

Author Comment

by:linkcube1
ID: 35467872
ok thanks for the tips guys!
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35468023
Glad to help!
and nice advice jdavistx!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question