Click to See Complete Forum and Search --> : Adding cells values
Andalus_Net
May 8th, 2003, 12:44 PM
Hi all,
How can I get the average of all the values of a column from datagrid1 and display them in a new datagrid2 located at the bottom of datagrid1?
Thank you!
coolbiz
May 9th, 2003, 11:08 AM
Loop through each rows in datagrid1 and sum up the values. Then divide the total with the total number of rows in datagrid1. Wallaa... you get the average. Then display it on datagrid2.
-Cool Bizs
Andalus_Net
May 26th, 2003, 10:28 AM
Tried the following code but the problem: it doesn't compute the verage for each column, it keeps adding cells values..:
Dim i, j, sum, avg As Integer
For i = 0 To dt.Rows.Count - 1
For j = 1 To dt.Columns.Count - 1
sum += dt.Rows(i)(dt.Columns(j))
MsgBox(sum)
Next
avg = sum / (dt.Rows.Count)
coolbiz
June 15th, 2003, 10:18 PM
Not sure if you still need this (too busy w/ work lately). To find the avg of each col, you should have the outer FOR loop to iterate the columns and loop through the rows in the inner FOR loop. So the code should look like:
Dim i, j, sum, avg As Integer
For i = 0 To dt.Columns.Count - 1
' reset sum
sum = 0
For j = 0 To dt.Rows.Count - 1
sum += dt.Rows(j)(dt.Columns(i))
Next
' display the sum and average
avg = sum/dt.Rows.Count
MsgBox("Sum: " & CStr(sum) & " Avg: " & CStr(avg))
Next
-Cool Bizs
Andalus_Net
June 16th, 2003, 12:17 PM
That was very helpful!
I got the missing piece.
The question now is how to find the Minimum and Max values in the same datagrid. May be I should loop through the rows and compare the values, then store the Min value... but how can I store it?
Thank you!
Cordially,
coolbiz
June 16th, 2003, 09:36 PM
Are you looking for MIN and MAX of all the values or per columns?
For per col:
Dim i, j, min as long, max as long
For i = 0 To dt.Columns.Count - 1
' reset max and min
min = 0
max = 0
For j = 0 To dt.Rows.Count - 1
if (dt.Rows(j)(dt.Columns(i)) < min) then min = dt.Rows(j)(dt.Columns(i))
if (dt.Rows(j)(dt.Columns(i)) > max) then max = dt.Rows(j)(dt.Columns(i))
Next
' display the min and max
MsgBox("Min: " & CStr(min) & " Max: " & CStr(max))
Next
To make it work for all the values, just comment out the RESET lines.
-Cool Bizs
Andalus_Net
June 17th, 2003, 06:48 AM
Got it, thanks,
Actually, I need to set Min = 9999999..., becaus there may not be a value= 0 in datagrid.
do u have any idea about printing a chart.?
(I use ChatFX for .Net, initially it looks good.)
Tanx
coolbiz
June 17th, 2003, 08:26 PM
I have not really used one but ComponentOne has a charting component. There are some other free/shareware components out there that do a decent job in printing charts.
-Cool Bizs
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.