Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi to all........

Can anyone help on how to sumup a specific column of a listview
and pass its value to a textbox in VB.NET?

Hope someone can help me....
Any help will be appreciate...

pls...help.... :sigh:
Posted
Updated 30-Sep-17 17:16pm

The better solution...and cleaner one is:

VB
Dim dblTotal as Double = 0
Dim dblTemp as Double

For Each lvItem As ListViewItem In ListView1.Items
   If Double.TryParse(lvItem.SubItems(2).Text, dblTemp) Then
       dblTotal += dblTemp
   End If
Next

TextBox1.Text = dblTotal


But you have to know that the first column is also a subItem so the code above would sum up the values in column 3. I also threw in the TryParse method because you need to check and make sure that there are actually doubles in that column. (Of course if it's databound and the column is a Double column, then you wouldn't need that.
 
Share this answer
 
Comments
Dalek Dave 24-May-10 4:02am    
OK, your code is neater!
I wasn't so far off though, considering I ran from the top of my head.

Worth an upvote for elegance that my code lacked .
Member 13439014 1-Oct-17 0:01am    
sir in solution 1 where do I put that code ?
Simple way is to loop through all the rows and get a total that way.

Then pass the total to a text box.

VB
Dim Li as ListViewItem
Dim dblTotal As Double

For Each Li In ListView1.Items
 If ListView1.Items.Count = 0 Then
  dblTotal = 0
  
  Else
dblTotal = dblTotal + CCur(Li.SubItems(4))

 End If
Next



I think should work (obviously edit for your needs!)

Mark up if helpful.
 
Share this answer
 
v3
Comments
Johnny J. 21-May-10 7:24am    
I think you've been writing off the top of your head, Dave. Shouldn't it be:

Dim Li as ListViewItem
Dim dblTotal As Double

For Each Li In ListView1.Items
If ListView1.Items.Count = 0 Then
dblTotal = 0 Else dblTotal = dblTotal + CCur(Li.SubItems(4)) End If Next ???
Dalek Dave 21-May-10 7:38am    
Guilty as charged.

I did just jot it down 'on the hoof' as it were.
William Winner 21-May-10 12:42pm    
When you declare a new Double, does it always start at 0? I know that (back in the day) when I was learning C++, they told us we always had to initialize our ints and doubles, and what not because declaring a new double just assigned it a place in memory and that place could have any value in it. But is that a change with C#?

If not, what would happen in the case that there are no items with your code?

The code within the For Each Li in ListView1.Items would never actually run, which would mean that dblTotal would never have its value changed. you should never actually get to the line dblTotal = 0 because you'll never enter the For each block if there are no items. Wouldn't the better option be:

Dim dblTotal as Double = 0

For Each Li in ListView1.Items
dblTotal += CCur(Li.SubItems(4))
Next

For that matter, why would you use CCur to convert an item to a double? That has to do another conversion to convert that currency type to a double.
Dalek Dave 24-May-10 3:57am    
William, having looked at it again, I see you are probably right, but as I said I just banged it out without checking too deeply.

I think my code would work (with a few tweaks), but agree it is a bit clumsy.

Also, CCur came about by force of habit! I am an Accountant after all, and tend to have to put that in most things I do! :)

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