Dim li As ListItem
Dim dTotal As Integer
For Each li In ListView1.ListItems
dTotal = Val(dTotal + CInt(li.SubItems(3)))
Next
txtgrade.Text = CStr(dTotal)
'returns the total value

data in LIST VIEW:

90
8
50
45

I used the codes above but I can only retrieve the values (45) ?? what seems to be the problem ..can anyone help me ... I'am a newbie ... I need to sum the columns in the list view the answer should be [193]

I used the codes above but I can only retrieve the values (45) ?? what seems to be the problem ..can anyone help me ... I'am a newbie ... I need to sum the columns in the list view the answer should be [193]

it cause you just take the value of sub item with index 3. dTotal = Val(dTotal + CInt(li.SubItems(3))) you should to take all sub item. you can add a counter to help.

Dim li As ListItem
Dim dTotal As Integer
dim i as integer
i = 0
For Each li In ListView1.ListItems
dTotal = Val(dTotal + CInt(li.SubItems(i)))
i = i + 1
Next
txtgrade.Text = CStr(dTotal)
'returns the total value

Edited 5 Years Ago by Jx_Man: typo

Dim li As ListItem
Dim dTotal As Integer
dim i as integer
i = 0
For Each li In ListView1.ListItems
dTotal = Val(dTotal + CInt(li.SubItems(i)))
i = i + 1
Next
txtgrade.Text = CStr(dTotal)
'returns the total value

there is an error..it says that "invalid property value" at ((li.subitems(i))

Dim li As ListItem
Dim dTotal As Integer
Dim j As Integer
For j = 0 To ListView1.ListItems.Count - 1
For Each li In ListView1.ListItems
dTotal = Val(dTotal + CInt(li.SubItems(j)))
j = j + 1
Next
Next j

txtgrade.Text = CStr(dTotal)

same error occur at "(li.SubItems(j)))" invalid propery value" :sad:

you said you got the result is 45, so i modified it without testing it.
counter is not related with property of control..

are your code running from the start?
actually you can directly sum values when you retrieve data from database use SUM funtion.

Edited 5 Years Ago by Jx_Man: n/a

Dim li As ListItem
Dim dTotal As Integer
Dim j As Integer

For j = 0 To ListView1.ListItems.Count
For Each li In ListView1.ListItems
dTotal = dTotal + Val(li.SubItems(j))
j = j + 1
Next
Next j

txtgrade.Text = Val(dTotal)

T___T

Dim li As ListItem
Dim dTotal As Integer
Dim j As Integer
For j = 1 To ListView1.ListItems.Count
For Each li In ListView1.ListItems
dTotal = dTotal + Val(li.SubItems(j))
j = j + 1
Next
Next j

data in LIST VIEW:

90
8
50
45


i do this code.. but the output is "8"

try this :

Dim i As Integer
    Dim dTotal As Double
 
    For i = 1 To ListView1.ListItems.Count
        dTotal = dTotal + ListView1.ListItems(i).SubItems(1)
    Next
    txtgrade.Text = Val(dTotal)
For j = 1 To ListView1.ColumnHeaders.Count - 1
For I = 1 To ListView1.ListItems.Count
Subtotal = Subtotal + Val(ListView1.ListItems.Item(I).SubItems(j))
Next I
Next j
txtgrade.Text = Subtotal

list view values:
90
8
50
45

this code works on me but ...i can only add the values 8 + 50 + 45 =103

i can't add the 90 T_T when i change the i to 0 in "For I = 1 To ListView1.ListItems.Count" an error occur index out of bounds ...

This article has been dead for over six months. Start a new discussion instead.