-
Mar 19th, 2018, 04:10 PM
#1
Thread Starter
Member
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
-
Mar 19th, 2018, 04:29 PM
#2
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.
-
Mar 19th, 2018, 04:34 PM
#3
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
-
Mar 19th, 2018, 04:35 PM
#4
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.
-
Mar 19th, 2018, 04:36 PM
#5
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
-
Apr 8th, 2018, 10:24 AM
#6
Thread Starter
Member
Re: Sorting and averaging in datagridview
hi all,
any one sussed this out yet
LL
Last night I dreamt I went to Manderley again
-
Apr 8th, 2018, 06:57 PM
#7
Re: Sorting and averaging in datagridview
Originally Posted by limelight
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.
-
Apr 8th, 2018, 08:49 PM
#8
Re: Sorting and averaging in datagridview
Originally Posted by jmcilhinney
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.
-
Apr 8th, 2018, 08:57 PM
#9
Re: Sorting and averaging in datagridview
Originally Posted by OptionBase1
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?
-
Apr 9th, 2018, 06:04 AM
#10
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|