0
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]

3
Contributors
15
Replies
19
Views
6 Years
Discussion Span
Last Post by Jx_Man
0

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 by Jx_Man: typo

0
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))

0
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:

0

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

0

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

Edited by Jx_Man: n/a

0
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

0
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"

0

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)
0
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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.