[RESOLVED] Adding Up Numbers In ListView Column
I found this from a Google search and I need the VB.NET equivalent to Martin Liss' code
Code:
Dim lngIndex As Long
Dim lngTot As Long
For lngIndex = 1 To lv.ListItems.Count
lngTot = lngTot + lv.ListItems(lngIndex).SubItems(1)
Next
MsgBox lngTot
I have
vb.net Code:
Dim intRptTotal As Integer = 0
For i As Integer = 1 To ListView1.Items.Count
intRptTotal = intRptTotal + CInt(ListView1.Items(i).SubItems(1))
Next
But, I'm getting the error
Quote:
Originally Posted by Option Strict
Value of type 'System.Windows.Forms.ListViewItem.ListViewSubItem' can not be converted to 'Integer'
Basically, I need to total up a number column in a ListView.
Re: Adding Up Numbers In ListView Column
You are getting a ListViewItem.ListViewSubItem here:
vb.net Code:
ListView1.Items(i).SubItems(1)
Once you've got that object, you get the text it displays from its Text property.
1 Attachment(s)
Re: Adding Up Numbers In ListView Column
I did add the Text property to it originally, but that gave me an error.
Re: Adding Up Numbers In ListView Column
The Listview in .NET is 0-based (VB6 is 1-based), so if you have 53 items, then loop from 0 to 52. ListView1.Items.Count will return 53, which means you're looping from 0 to 53 and index 53 doesn't exist.
You don't have to do "intRptTotal = intRptTotal + ....", but you can do "intRptTotal += ..." instead.
vb.net Code:
Dim intRptTotal As Integer = 0
For i As Integer = 0 To lvwReportCount.Items.Count - 1
intRptTotal += CInt(lvwReportCount.Items(i).SubItems(1).Text)
Next
Alternatively, you can also do a for each loop.
vb.net Code:
For Each lvw As ListViewItem In lvwReportCount.Items
intRptTotal += CInt(lvw.SubItems(1).Text)
Next
Re: Adding Up Numbers In ListView Column
It's 0 based so you need to offset by 1 from the count.
Code:
For i As Integer = 0 To lvwReportCount.Items.Count - 1
Re: Adding Up Numbers In ListView Column
what chris wrote...i added another line which will help u accomplish what u r after...
vb Code:
Dim intRptTotal As Integer = 0
For i As Integer = 0 To lvwReportCount.Items.Count - 1
intRptTotal += CInt(lvwReportCount.Items(i).Text)
intRptTotal += CInt(lvwReportCount.Items(i).SubItems(1).Text)
Next
vb Code:
For Each lvw As ListViewItem In lvwReportCount.Items
intRptTotal += CInt(lvw.Text)
intRptTotal += CInt(lvw.SubItems(1).Text)
Next
Re: Adding Up Numbers In ListView Column
Quote:
Originally Posted by
Chris001
The Listview in .NET is 0-based (VB6 is 1-based), so if you have 53 items, then loop from 0 to 52. ListView1.Items.Count will return 53, which means you're looping from 0 to 53 and index 53 doesn't exist.
That is where I was going wrong...I was still using a 1-based loop. :rolleyes: Thanks.
Quote:
Originally Posted by
Chris001
You don't have to do "intRptTotal = intRptTotal + ....", but you can do "intRptTotal += ..." instead.
I know I can do it that way, but I don't, and I won't.
I find it far more clear to use variable = variable + newvariable
Old dog...new tricks...etc. :D