Hi,
I have some number 1 to 5. How to find top 3 number in that range ?
For example, in the range of 1 to 5 , I want to show only 1 to 3 in the the text box.
Your definition of "top 3" seems to be "the first 3". My definition of "top 3" would be the three largest. If this is a homework assignment I suggest you get a clear definition.
Yup , it was "the first 3" . Sorry for mistake.
Actually that numbers, I extracted from DatagridView. Then I need to find the "first 3".
I manage to extract it and scan the maximum. But I still stuck to figured it out to find the "first 3"
Here my first code :
Dim aa3 As Decimal = 0
Dim bb3 As Decimal = 0
Dim cc3 As Decimal = 0
Dim dd3 As Decimal = 0
Dim ee3 As Decimal = 0
aa3 = Convert.ToDecimal(DataGridView1.Rows(4 - 2).Cells(4).Value)
bb3 = Convert.ToDecimal(DataGridView1.Rows(52 - 2).Cells(4).Value)
cc3 = Convert.ToDecimal(DataGridView1.Rows(100 - 2).Cells(4).Value)
dd3 = Convert.ToDecimal(DataGridView1.Rows(148 - 2).Cells(4).Value)
ee3 = Convert.ToDecimal(DataGridView1.Rows(196 - 2).Cells(4).Value)
' Create a list of Long values.
Dim longs3 As New List(Of Double)(New Double() _
{aa3, bb3, cc3, dd3, ee3})
' Get the maximum value in the list.
Dim max3 As Double = longs3.Max()
' Display the result.
TextBox7.Text = Format(max3, "0.00")
Hi Reverend Jim,
I manage to find the first 3 numbers. But, when I put them in a texbox, it become more messy . How to show in 3 textbox ?
Here my 2nd code:
Dim Baa3 As Decimal = 0
Dim Bbb3 As Decimal = 0
Dim Bcc3 As Decimal = 0
Dim Bdd3 As Decimal = 0
Dim Bee3 As Decimal = 0
Baa3 = Convert.ToDecimal(DataGridView1.Rows(4 - 2).Cells(4).Value)
Bbb3 = Convert.ToDecimal(DataGridView1.Rows(52 - 2).Cells(4).Value)
Bcc3 = Convert.ToDecimal(DataGridView1.Rows(100 - 2).Cells(4).Value)
Bdd3 = Convert.ToDecimal(DataGridView1.Rows(148 - 2).Cells(4).Value)
Bee3 = Convert.ToDecimal(DataGridView1.Rows(196 - 2).Cells(4).Value)
' Create a list of Long values.
Dim longs3 As New List(Of Double)(New Double() _
{Baa3, Bbb3, Bcc3, Bdd3, Bee3})
' first three values.
Dim topThreeGrades As IEnumerable(Of Double) = _
longs3 _
.OrderByDescending(Function(grade) grade) _
.Take(3)
' Display the results.
Dim output As New System.Text.StringBuilder("The first three values are:" & vbCrLf)
For Each grade As Double In topThreeGrades
output.AppendLine(grade)
Next
TextBox1.Text = output.ToString()
TextBox2.Text = output.ToString()
TextBox3.Text = output.ToString()
You can get the required numbers from the grid by
Dim arr(4) As Integer
Dim i As Integer = 0
For Each r As Integer In {2, 50, 98, 146, 194}
arr(i) = DataGridView1.Rows(r).Cells(4).Value
i += 1
Next
and you can sort the numbers by
Array.Sort(arr)
copy the numbers to the textboxes by
TextBox1.Text = arr(0)
.
.
I used integers for my example. Modify as required for your data type.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.