ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 2754|ตอบกลับ: 0

[VB.NET] การจัดอันดับที่มีค่าคะแนนเท่ากัน (Duplicate Ranking)

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6030




ความหมายของการจัดอันดับที่มีค่าคะแนนเท่ากัน ก็จะคล้ายๆกับการเล่นกอล์ฟนั่นแหละครับ เช่นตามภาพ อันดับที่ 9 มีคะแนนเท่ากัน 3 คน ดังนั้นอันดับที่ 10 และ 11 ก็เลยไม่มี พอนับคนถัดไปก็จะไล่เรียงไปในลำดับที่ 12 ...  


มาดูโค้ดกันเถอะ ...
  1. ' / ----------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / More info: http://www.g2gnet.com/webboard
  8. ' /
  9. ' / Purpose: How to sort duplicate ranking in DataGridView without DataBase.
  10. ' / Microsoft Visual Basic .NET (2010)
  11. ' /
  12. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / ----------------------------------------------------------------
  15. Public Class frmRankingDuplicate
  16.     Dim dt As DataTable
  17.     Dim MaxRow As Integer = 15

  18.     ' / ----------------------------------------------------------------
  19.     '// Initialize DataGridView @Run Time
  20.     Private Sub InitGrid()
  21.         With dgvData
  22.             .RowHeadersVisible = False
  23.             .AllowUserToAddRows = False
  24.             .AllowUserToDeleteRows = False
  25.             .AllowUserToResizeRows = False
  26.             .MultiSelect = True
  27.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  28.             .ReadOnly = True
  29.             .Font = New Font("Tahoma", 9)
  30.             ' Autosize Column
  31.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  32.             .AutoResizeColumns()
  33.             '// Even-Odd Color
  34.             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
  35.             ' Adjust Header Styles
  36.             With .ColumnHeadersDefaultCellStyle
  37.                 .BackColor = Color.Navy
  38.                 .ForeColor = Color.Black ' Color.White
  39.                 .Font = New Font("Tahoma", 9, FontStyle.Bold)
  40.             End With
  41.         End With
  42.     End Sub

  43.     Private Sub frmRankingDuplicate_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  44.         Me.Dispose()
  45.         Application.Exit()
  46.     End Sub

  47.     ' / ----------------------------------------------------------------
  48.     Private Sub frmRankingDuplicate_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  49.         Call InitGrid()
  50.         Call Ranking()
  51.     End Sub

  52.     ' / ----------------------------------------------------------------
  53.     Private Sub Ranking()
  54.         Dim ds As New DataSet()
  55.         dt = New DataTable
  56.         dt.Columns.Add("Student", GetType(String))
  57.         dt.Columns.Add("Subject A", GetType(Integer))
  58.         dt.Columns.Add("Subject B", GetType(Integer))
  59.         dt.Columns.Add("Subject C", GetType(Integer))
  60.         dt.Columns.Add("Average", GetType(Double))
  61.         dgvData.DataSource = dt
  62.         '//
  63.         Randomize()
  64.         Dim RandomClass As New Random()
  65.         For i As Integer = 0 To MaxRow - 1
  66.             Dim dr As DataRow = dt.NewRow()
  67.             '// RandomClass
  68.             dr(0) = "Student " & i + 1
  69.             dr(1) = RandomClass.Next(0, 100)
  70.             dr(2) = RandomClass.Next(0, 100)
  71.             dr(3) = RandomClass.Next(0, 100)
  72.             Dim SumCol As Double = 0
  73.             For iCol As Byte = 1 To 3 'MaxCol
  74.                 SumCol = SumCol + dr(iCol)
  75.             Next
  76.             dr(4) = Format(SumCol / 3, "0.00")
  77.             '//
  78.             dt.Rows.Add(dr)
  79.         Next
  80.         '// Calcualte ranking
  81.         dgvData.DataSource = RankingDuplicate(dt, "Average")
  82.     End Sub

  83.     ' / ----------------------------------------------------------------
  84.     Public Function RankingDuplicate(dt As DataTable, fld As String) As DataTable
  85.         '// Descending (Z --> A)
  86.         Dim rankingDt = (From row In dt.AsEnumerable() Order By row.Field(Of Double)(fld) Descending.CopyToDataTable())
  87.         rankingDt.Columns.Add("Ranking")
  88.         Dim rank As Integer = 1
  89.         Dim count As Integer = 1
  90.         For i As Integer = 0 To rankingDt.Rows.Count - 2
  91.             rankingDt.Rows(i)("Ranking") = rank
  92.             '// If not duplicate value then increment +1.
  93.             If rankingDt.Rows(i)(fld).ToString() <> rankingDt.Rows(i + 1)(fld).ToString() Then
  94.                 rank += 1
  95.                 rank = count + 1
  96.             End If
  97.             count += 1
  98.         Next
  99.         rankingDt.Rows(rankingDt.Rows.Count - 1)("Ranking") = rank
  100.         Return rankingDt
  101.     End Function

  102.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  103.         Me.Close()
  104.     End Sub

  105.     Private Sub btnProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnProcess.Click
  106.         Call Ranking()
  107.     End Sub
  108. End Class
คัดลอกไปที่คลิปบอร์ด



ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-19 13:02 , Processed in 0.414327 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้