Results 1 to 10 of 10

Thread: Sorting and averaging in datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Sorting and averaging in datagridview

    hi all,
    I have been working with the datagridview and in one column I have the student scores for the last ten exams.
    I am looking for a way to find the five best scores out of the last ten and then find the average of these five.Then place the figure into a textbox.
    I have code that will loop through the 10 score marks and average them all,but I cannot figure out a way of just getting the best 5.
    I assume some sort of sort process will have to be undertaken,but I am not to sure how to do this.Here is the code to average the last ten scores out if this is any help

    Code:
     Dim sum As Double = 0
            Dim d As Double
            On Error Resume Next
            For d = 0 To 9
                sum +=
                DataGridView1.Rows(d).Cells("score").Value()
                On Error Resume Next
            Next
            NumericUpDown6.Text = sum /10
    Regards
    LL
    Last edited by Shaggy Hiker; Mar 19th, 2018 at 04:34 PM. Reason: Added Code Tag
    Last night I dreamt I went to Manderley again

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Sorting and averaging in datagridview

    Try the following code:
    Code:
    Dim top10() As Double = (From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow) Select Convert.ToDouble(row.Cells("score").Value)).OrderByDescending(Function(value) value).Take(10).ToArray()
    TextBox1.Text = top10.Average().ToString
    What this does is loop through each row in the DataGridView and returns the converted value from the score column (as a Double), sorts the results from greatest to smallest, takes the top 10, and then averages the results.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Sorting and averaging in datagridview

    Yeah, but he wants the top 5, so I think Take(5) would be more correct. He was looking at getting the top 10, but only averaging the top 5. Therefore, if the top 10 is needed for any reason, that code gives you the top 10 with the first line (the top10 array), but out of that, you would still want to average just the top 5, so the second line would need to Take(5) from top10, and average those.
    My usual boring signature: Nothing

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Sorting and averaging in datagridview

    See originally, I had thought that he wanted the top 5 and so I provided the code for top 5. Then I re-read it and somehow though he wanted top 10, and so I edited the code to do it for top 10. Now I'm re-reading it again and it looks like he wants top 5.

    I'm a mess today.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Sorting and averaging in datagridview

    I can only assume that DDay is looking at this thread, because I went into the first post to edit it to add the [CODE][/CODE] tags, but didn't actually do anything, since the tags were already there. The message says that I edited the code, with the message about adding code tags, but I didn't add the message, and I didn't actually edit anything.

    Therefore, I think I just stole credit for something DDay did...with the help of the forum software.

    EDIT: Yep, DDay is here. I'm pretty fatigued today, too.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Re: Sorting and averaging in datagridview

    hi all,
    any one sussed this out yet
    LL
    Last night I dreamt I went to Manderley again

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Sorting and averaging in datagridview

    Quote Originally Posted by limelight View Post
    hi all,
    any one sussed this out yet
    LL
    It seems like the OP didn't provide adequate explanation as those helping didn't seem sure of exactly what they wanted and they didn't return to clarify. Maybe you should try posting your own question in your own thread with your own FULL and CLEAR explanation of your problem and then we can concentrate on that, rather than an unclear question from someone who seems to have abandoned it for whatever reason.

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Sorting and averaging in datagridview

    Quote Originally Posted by jmcilhinney View Post
    It seems like the OP didn't provide adequate explanation as those helping didn't seem sure of exactly what they wanted and they didn't return to clarify. Maybe you should try posting your own question in your own thread with your own FULL and CLEAR explanation of your problem and then we can concentrate on that, rather than an unclear question from someone who seems to have abandoned it for whatever reason.
    The post you just responded to was made by the person who started this thread.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Sorting and averaging in datagridview

    Quote Originally Posted by OptionBase1 View Post
    The post you just responded to was made by the person who started this thread.
    Oopsy! In that case, what exactly has not been provided so far that you still need?

  10. #10
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: Sorting and averaging in datagridview

    a possible solution
    Code:
          Dim col As String= "Score", firstrow = 0
           Dim takeRows As Integer = 5
           DataGridView1.Sort(DataGridView1.Columns(col), System.ComponentModel.ListSortDirection.Descending)
            Dim values = DataGridView1.Rows.
                OfType(Of DataGridViewRow)().
                Select(Of Single)(Function(r) Single.Parse(r.Cells(col).Value.ToString())).
                Skip(firstrow).
                Take(takeRows)
            Dim max = values.Max().ToString
            Dim min = values.Min().ToString
            Dim avg = values.Average().ToString
            MessageBox.Show("Average " + avg + ", Min " + min + ", Max " + max)

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