|
-
May 8th, 2003, 12:44 PM
#1
Get "Average" of cells values in DataGrid
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!
Last edited by Andalus_Net; May 8th, 2003 at 12:50 PM.
-
May 9th, 2003, 11:08 AM
#2
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
-
May 26th, 2003, 10:28 AM
#3
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)
Last edited by Andalus_Net; May 26th, 2003 at 10:33 AM.
-
June 15th, 2003, 10:18 PM
#4
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:
Code:
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
-
June 16th, 2003, 12:17 PM
#5
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,
Last edited by Andalus_Net; June 16th, 2003 at 12:54 PM.
-
June 16th, 2003, 09:36 PM
#6
Are you looking for MIN and MAX of all the values or per columns?
For per col:
Code:
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
-
June 17th, 2003, 06:48 AM
#7
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
-
June 17th, 2003, 08:26 PM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|