CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Location
    Al_Andalus
    Posts
    12

    Question 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.

  2. #2
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    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

  3. #3
    Join Date
    Apr 2003
    Location
    Al_Andalus
    Posts
    12
    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.

  4. #4
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    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

  5. #5
    Join Date
    Apr 2003
    Location
    Al_Andalus
    Posts
    12
    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.

  6. #6
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    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

  7. #7
    Join Date
    Apr 2003
    Location
    Al_Andalus
    Posts
    12
    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

  8. #8
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    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
  •  





Click Here to Expand Forum to Full Width

Featured