Results 1 to 10 of 10

Thread: [RESOLVED] Data Grid View Sorting Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    12

    Resolved [RESOLVED] Data Grid View Sorting Help

    basically i had to write a program where a user enters the amount of countrys taking part in the Olympics and enters there medals. i should then be able to sort the country's out with the best country being the one with the most amount of gold meals. my problem is its not sorting it out and oping someone could help. i also have a problem where i automatically want the program t work out the total by it self and don't know where to put it.
    thanks in advance

    data Code:
    1. Public Class fmldn
    2.     Structure Olympicsinfo
    3.         Dim country As String
    4.         Dim gold As String
    5.         Dim silver As String
    6.         Dim bronze As String
    7.         Dim total As String
    8.     End Structure
    9.  
    10.     Dim Olympics() As Olympicsinfo
    11.     Dim N As Integer
    12.  
    13.     Private Sub btnenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenter.Click
    14.         N = InputBox("Enter the number of Countrys")
    15.         ReDim Olympics(0 To N - 1)
    16.  
    17.         For I = 0 To N - 1
    18.             Olympics(I).country = InputBox("Enter the Country")
    19.             Olympics(I).gold = InputBox("Enter the Amount of Gold Medals")
    20.             Olympics(I).silver = InputBox("Enter the Amount of Silver Medals")
    21.             Olympics(I).bronze = InputBox("Enter the Amount of Bronze Medals")
    22.             Olympics(I).total = InputBox("Enter the Total Amount Of Medals")
    23.         Next
    24.  
    25.     End Sub
    26.  
    27.  
    28.     Private Sub Display()
    29.         DGVolympics.Rows.Clear()
    30.  
    31.         For I = 0 To N - 1
    32.             dgvolympics.Rows.Add(Olympics(I).country, Olympics(I).gold,
    33.             Olympics(I).silver, Olympics(I).bronze, Olympics(I).total)
    34.         Next
    35.     End Sub
    36.  
    37.     Private Sub btnview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnview.Click
    38.         Call Display()
    39.     End Sub
    40.  
    41.     Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
    42.         Dim Swap As Boolean
    43.         Dim temp As Olympicsinfo
    44.         'Perform the Bubble Sort
    45.         Do
    46.             Swap = True
    47.             For I = 0 To N - 2
    48.                 If Olympics(I).gold > Olympics(I + 1).gold Then
    49.                     Swap = True
    50.                     temp = Olympics(I)
    51.                     Olympics(I) = Olympics(I + 1)
    52.                     Olympics(I + 1) = temp
    53.                 End If
    54.             Next I
    55.             N = N - 1
    56.         Loop Until (Not (Swap) Or (N = 1))
    57.     End Sub
    58.  
    59. End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Grid View Sorting Help

    do you have to use a bubble sort?
    here's an example with an IComparer class:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Structure Olympicsinfo
    4.         Dim country As String
    5.         Dim gold As Integer
    6.         Dim silver As Integer
    7.         Dim bronze As Integer
    8.         Dim total As Integer
    9.     End Structure
    10.  
    11.     Dim Olympics() As Olympicsinfo
    12.     Dim N As Integer
    13.  
    14.     Private Sub btnenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenter.Click
    15.         Integer.TryParse(InputBox("Enter the number of Countrys"), N)
    16.         ReDim Olympics(N - 1)
    17.  
    18.         For I = 0 To N - 1
    19.             Olympics(I).country = InputBox("Enter the Country")
    20.             Integer.TryParse(InputBox("Enter the Amount of Gold Medals"), Olympics(I).gold)
    21.             Integer.TryParse(InputBox("Enter the Amount of Silver Medals"), Olympics(I).silver)
    22.             Integer.TryParse(InputBox("Enter the Amount of Bronze Medals"), Olympics(I).bronze)
    23.             Olympics(I).total = Olympics(I).gold + Olympics(I).silver + Olympics(I).bronze
    24.         Next
    25.  
    26.     End Sub
    27.  
    28.  
    29.     Private Sub Display()
    30.         DGVolympics.Rows.Clear()
    31.  
    32.         For I = 0 To N - 1
    33.             dgvolympics.Rows.Add(Olympics(I).country, Olympics(I).gold, _
    34.             Olympics(I).silver, Olympics(I).bronze, Olympics(I).total)
    35.         Next
    36.     End Sub
    37.  
    38.     Private Sub btnview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnview.Click
    39.         Call Display()
    40.     End Sub
    41.  
    42.     Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
    43.         dgvolympics.Sort(New comparer)
    44.     End Sub
    45.  
    46. End Class
    47.  
    48. Public Class comparer
    49.     Implements IComparer
    50.  
    51.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    52.         Dim rowX As DataGridViewRow = DirectCast(x, DataGridViewRow)
    53.         Dim rowY As DataGridViewRow = DirectCast(y, DataGridViewRow)
    54.  
    55.         Dim cellX As DataGridViewCell = DirectCast(rowX.Cells("Gold"), DataGridViewCell)
    56.         Dim cellY As DataGridViewCell = DirectCast(rowY.Cells("Gold"), DataGridViewCell)
    57.  
    58.         Return CInt(cellY.Value).CompareTo(CInt(cellX.Value))
    59.     End Function
    60.  
    61. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    12

    Re: Data Grid View Sorting Help

    unfortunately i can only use bubble sort
    i haven't learnt ICompare yet

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Grid View Sorting Help

    vb Code:
    1. Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
    2.  
    3.     Dim rows() As DataGridViewRow = dgvolympics.Rows.Cast(Of DataGridViewRow).Where(Function(r) Not r.IsNewRow).ToArray
    4.     'bubble sort
    5.     For outer As Integer = rows.GetUpperBound(0) To 0 Step -1
    6.         For inner As Integer = 0 To outer - 1
    7.             If CInt(rows(inner).Cells("Gold").Value) < CInt(rows(inner + 1).Cells("Gold").Value) Then
    8.                 Dim temp As DataGridViewRow = rows(inner)
    9.                 rows(inner) = rows(inner + 1)
    10.                 rows(inner + 1) = temp
    11.             End If
    12.         Next
    13.     Next
    14.  
    15.     dgvolympics.Rows.Clear()
    16.     dgvolympics.Rows.AddRange(rows)
    17.  
    18. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    12

    Re: Data Grid View Sorting Help

    i have no idea what you've done there but thanks

    just a few more things,
    the total number of medals needs to work out by itself
    and something that If two countries have the same number of gold medals, the number of silver, and then bronze medals if necessary are compared

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Grid View Sorting Help

    read post #2 again. it does the totals

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Grid View Sorting Help

    Quote Originally Posted by MrZubs View Post
    i have no idea what you've done there but thanks
    it's a bubble sort on the datagridviewrows. here's how to sort firstBy Gold, thenBy Silver, thenBy Bronze:

    vb Code:
    1. Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
    2.  
    3.     Dim rows() As DataGridViewRow = dgvolympics.Rows.Cast(Of DataGridViewRow).Where(Function(r) Not r.IsNewRow).ToArray
    4.     'bubble sort
    5.     For outer As Integer = rows.GetUpperBound(0) To 0 Step -1
    6.         For inner As Integer = 0 To outer - 1
    7.             If CInt(rows(inner).Cells("Gold").Value) < CInt(rows(inner + 1).Cells("Gold").Value) Then
    8.                 Dim temp As DataGridViewRow = rows(inner)
    9.                 rows(inner) = rows(inner + 1)
    10.                 rows(inner + 1) = temp
    11.             ElseIf CInt(rows(inner).Cells("Gold").Value) = CInt(rows(inner + 1).Cells("Gold").Value) Then
    12.                 If CInt(rows(inner).Cells("Silver").Value) < CInt(rows(inner + 1).Cells("Silver").Value) Then
    13.                     Dim temp As DataGridViewRow = rows(inner)
    14.                     rows(inner) = rows(inner + 1)
    15.                     rows(inner + 1) = temp
    16.                 ElseIf CInt(rows(inner).Cells("Silver").Value) = CInt(rows(inner + 1).Cells("Silver").Value) Then
    17.                     If CInt(rows(inner).Cells("Bronze").Value) < CInt(rows(inner + 1).Cells("Bronze").Value) Then
    18.                         Dim temp As DataGridViewRow = rows(inner)
    19.                         rows(inner) = rows(inner + 1)
    20.                         rows(inner + 1) = temp
    21.                     End If
    22.                 End If
    23.             End If
    24.         Next
    25.     Next
    26.  
    27.     dgvolympics.Rows.Clear()
    28.     dgvolympics.Rows.AddRange(rows)
    29.  
    30. End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    12

    Re: Data Grid View Sorting Help

    the totals part doesn't work
    it just puts everything you entered next to each other

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Grid View Sorting Help

    i changed the structure too. + the way the array is filled in btnenter_click

    vb Code:
    1. Structure Olympicsinfo
    2.     Dim country As String
    3.     Dim gold As Integer
    4.     Dim silver As Integer
    5.     Dim bronze As Integer
    6.     Dim total As Integer
    7. End Structure

    think about it: if you have 5 gold medals, what is 5? it's a number, a whole number, i.e. an integer + not a string:

    example:

    vb Code:
    1. 1 + 2 = 3 'integer addition
    2. "1" + "2" = "12" 'string concatenation

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    12

    Re: Data Grid View Sorting Help

    such a schoolboy error
    i had total changed into integer and just completely lost it :S
    well anyways thanks for your help
    REP+

Tags for this Thread

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