get columns totals from listview control

peterpiper

Member
Joined
Nov 6, 2013
Messages
5
Programming Experience
Beginner
I have the following piece of code which gets the sum of a column
in a listView and displays the value in a label on a c# form.

C#:
Decimal iSum = 0;
        foreach (ListViewItem o in this.listView3.Items)
        {
            iSum = iSum + Convert.ToDecimal(o.SubItems[1].Text);
        }
        label2.Text = iSum.ToString();
How can I get the sum of three columns and then display the values
in three labels on the forms. If someone can show me how to achieve this
Let me know if question is not clear
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
All summed together in one sum? Or three individual sums (one per column)?

For the first one, just repeat your iSum = iSum ... three times for the different columns. For the second one, use three sum variables and in the foreach do the summing like you do now.

Not behind a computer to create a sample code and typing on a tablet is a disaster.

// edit
Reading from a tablet also does not seem to be my strong point :( I now see that it's the second one.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,060
Location
Sydney, Australia
Programming Experience
10+
That's like saying that you know how to take a step but asking how to take three steps. If you know how to do something once then you don't need to be told how to do the sane thing three times. Of course, you only need one loop because each item contains all three columns.
 
Top Bottom