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!
Printable View
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!
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
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)
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:
-Cool BizsCode: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
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,
Are you looking for MIN and MAX of all the values or per columns?
For per col:
To make it work for all the values, just comment out the RESET lines.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
-Cool Bizs
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
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