Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, i have a list view, and i want to add all of the decimals inside of the listview and display the total in a label.

Ideally i want the total (in the label) to be updated each time the listview is updated. But right now i just want a working total to be displayed in the label.

Since im working in WPF the SubItems doesnt work, i've tried looking for the solutions around it but i get completely overwhelmed by what i find.

I've tried this.

C#
private void Button_Click_1(object sender, RoutedEventArgs e)
{

    decimal total = 0;

    foreach (ListViewItem lstViewItem in orderDetailsListView.Items)
    {

        foreach (ListViewItem o in orderDetailsListView.Items)
            total = total + Convert.ToDecimal(orderDetailsListView.Items.["Price"].Text);

        totalOutputLabel.Content = total;
    }
}



But it doesnt work, because an identifier is expected here...Items.["Price"]


I know its so simple and can be done in no time, but i've searched and search and every solution i have come across, is either VB, winforms, datagrids and the rare chance i find a solution for mine, it doesnt work due to parts of their code not being the same as mine.
Posted
Updated 5-Mar-15 4:21am
v4
Comments
Sergey Alexandrovich Kryukov 5-Mar-15 10:16am    
What have you tried so far?
—SA
[no name] 5-Mar-15 10:22am    
Updated the question.

1 solution

My suggestion would be to create a CustomListView control that is derived from the wpf ListView and override the OnItemsChanged protected method.

https://msdn.microsoft.com/en-us/library/system.windows.controls.listview.onitemschanged(v=vs.110).aspx[^]

Here you can verify NotifyCollectionChangedEventArgs Action property to see if a item was added, removed, replaced or reset and add the logic for calculating the total if necessary.

https://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedaction(v=vs.110).aspx[^]
 
Share this answer
 
Comments
[no name] 5-Mar-15 13:32pm    
I'm sorry, but your reply didn't help.
Adrian Vaideanu 5-Mar-15 14:05pm    
What difficulties did you encounter ?
[no name] 5-Mar-15 14:07pm    
I just didn't know what i was looking at, i didnt know where to start :( .. I'm not that experience in C# you see.

I may have made progress though, so ill ask for your advice here. Do you know how to sum an entire column of a listview in XAML and output it to a label?
Adrian Vaideanu 5-Mar-15 15:14pm    
You just have to iterate over the "Items" property of the ListView instance. Based on their type (double, float, etc.) do an explicit conversion before summing them up.
After calculating the sum, just set the "Content" property of your label control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900