Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Is somebody can help me with the following question?

How can I get the AVERAGE number from the column of ListView1.Items(ListView1.Items.Count - 1).SubItems.Add((reader("High").ToString) - (reader("Low").ToString)) and shows the AVERAGE number on the label.


Dim Cmd As SqlCommand = New SqlCommand(SelectCmd, conn)
Dim reader As SqlDataReader
reader = Cmd.ExecuteReader()

'**************************
Do While (reader.Read())

ListView1.Items.Add(reader("Id").ToString)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(reade("Date").ToString)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(reade("High").ToString)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(reader("Low").ToString)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add((reader("High").ToString) - (reader("Low").ToString))

Loop
'***************************
 reader.Close()
 conn.Close()

For example:
The column outputs will be:
1
3
5
2
8
________
Avg: 3.8

That will be the number i would like to have it on the label.

Thank you for your help!
Posted
Updated 20-Apr-10 6:24am
v3

This really isn't that difficult. I sure hope you're just starting out and actually in a class doing this.

You need to think through the problem logically. What do you need to do to solve it?

Well, you need to go through each item in the ListView and sum up their values.

Then, divide that value by the total number of items, right? What exactly are you unclear about?

VB
Dim sum as Long = 0

For Each lvItem as ListViewItem in ListView1.Items
  sum += CInt(item.SubItems(0).Text)
Next

Label1.Text = sum / ListView1.Items.Count


If you can't understand something as simple as that, maybe this isn't the profession for you.
 
Share this answer
 
How about
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(((reader(High)- (reader(Low)) / 2).ToString))


Or you could also post your SQL and have a new field added to the query.
 
Share this answer
 

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