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]

Recommended Answers

All 15 Replies

From where the values in the listview gets populated ?

Is there a database available ?

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

@debasidas yes that values are from the database

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.

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"

better do all sort of calculations at the DB level.

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 ...

did you try my code on the last post?

yes i have :3

can u post the screenshoot of your listview result..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.