Excel style number adding in listview column

DiverDan

Contributor
Joined
Jan 16, 2003
Messages
645
Location
Sacramento, CA
It would be soooo easy to total the results of calculations in 5 different listview columns if I hadn't opted for a clear row function....but I did. The results are now in 5 of the 10 columns Excel spread sheet style. Each column has a specific name i.e. lvwAmps, lvwkW, lvwHP, lvwkVa, and lvwBTU. (this is an electric engery calculator). I do not know how many rows are present at the time the user wants to calculate the totals for these 5 columns. So how do I total the results in the given columns?
 
Are these columns always the same ones? I.e., the same index columns are to be added each time? If so, you could loop through all the listviewitems in your listview 5 times, adding up a specified column index each time.
 
Thanks for your responce,

Yes, they are always the same columns. The first five columns are not required for the addition. And you idea sounds great...could you show an example?
 
This piece of code adds up all the numbers in the second column on a listview in Details mode.

Visual Basic:
        Dim l As ListViewItem
        Dim count As Integer

        count = 0
        For Each l In ListView1.Items
            count += Integer.Parse(l.SubItems(1).Text)
        Next

        MessageBox.Show(count.ToString())
 
Are you binding your ListView from a DataSet (not even sure if that's possible)? If so, the DataTable supports a Compute method to sum up values all at once.

-nerseus
 
No nerseus, the listview data is generated from calculations not a dataset. It acutally works very well and feels like Excel, except you cannot edit any of the colmuns except the first. This works very well in this case since the results are used for sizing transformers, circuit breakers, wire gauges, etc. per NFPA-NEC codes and regulations. Any user editing could have disasterious results resulting in lawyers. Pooh on that!

This is only my second major project with VB.Net and it is moving slowly but with wonderful results thanks to yourself, divil and the many others on this board.

Thanks again!
 
Back
Top