|
-
Mar 18th, 2012, 11:01 AM
#1
Thread Starter
New Member
[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:
Public Class fmldn
Structure Olympicsinfo
Dim country As String
Dim gold As String
Dim silver As String
Dim bronze As String
Dim total As String
End Structure
Dim Olympics() As Olympicsinfo
Dim N As Integer
Private Sub btnenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenter.Click
N = InputBox("Enter the number of Countrys")
ReDim Olympics(0 To N - 1)
For I = 0 To N - 1
Olympics(I).country = InputBox("Enter the Country")
Olympics(I).gold = InputBox("Enter the Amount of Gold Medals")
Olympics(I).silver = InputBox("Enter the Amount of Silver Medals")
Olympics(I).bronze = InputBox("Enter the Amount of Bronze Medals")
Olympics(I).total = InputBox("Enter the Total Amount Of Medals")
Next
End Sub
Private Sub Display()
DGVolympics.Rows.Clear()
For I = 0 To N - 1
dgvolympics.Rows.Add(Olympics(I).country, Olympics(I).gold,
Olympics(I).silver, Olympics(I).bronze, Olympics(I).total)
Next
End Sub
Private Sub btnview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnview.Click
Call Display()
End Sub
Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
Dim Swap As Boolean
Dim temp As Olympicsinfo
'Perform the Bubble Sort
Do
Swap = True
For I = 0 To N - 2
If Olympics(I).gold > Olympics(I + 1).gold Then
Swap = True
temp = Olympics(I)
Olympics(I) = Olympics(I + 1)
Olympics(I + 1) = temp
End If
Next I
N = N - 1
Loop Until (Not (Swap) Or (N = 1))
End Sub
End Class
-
Mar 18th, 2012, 12:01 PM
#2
Re: Data Grid View Sorting Help
do you have to use a bubble sort?
here's an example with an IComparer class:
vb Code:
Public Class Form1
Structure Olympicsinfo
Dim country As String
Dim gold As Integer
Dim silver As Integer
Dim bronze As Integer
Dim total As Integer
End Structure
Dim Olympics() As Olympicsinfo
Dim N As Integer
Private Sub btnenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenter.Click
Integer.TryParse(InputBox("Enter the number of Countrys"), N)
ReDim Olympics(N - 1)
For I = 0 To N - 1
Olympics(I).country = InputBox("Enter the Country")
Integer.TryParse(InputBox("Enter the Amount of Gold Medals"), Olympics(I).gold)
Integer.TryParse(InputBox("Enter the Amount of Silver Medals"), Olympics(I).silver)
Integer.TryParse(InputBox("Enter the Amount of Bronze Medals"), Olympics(I).bronze)
Olympics(I).total = Olympics(I).gold + Olympics(I).silver + Olympics(I).bronze
Next
End Sub
Private Sub Display()
DGVolympics.Rows.Clear()
For I = 0 To N - 1
dgvolympics.Rows.Add(Olympics(I).country, Olympics(I).gold, _
Olympics(I).silver, Olympics(I).bronze, Olympics(I).total)
Next
End Sub
Private Sub btnview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnview.Click
Call Display()
End Sub
Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
dgvolympics.Sort(New comparer)
End Sub
End Class
Public Class comparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim rowX As DataGridViewRow = DirectCast(x, DataGridViewRow)
Dim rowY As DataGridViewRow = DirectCast(y, DataGridViewRow)
Dim cellX As DataGridViewCell = DirectCast(rowX.Cells("Gold"), DataGridViewCell)
Dim cellY As DataGridViewCell = DirectCast(rowY.Cells("Gold"), DataGridViewCell)
Return CInt(cellY.Value).CompareTo(CInt(cellX.Value))
End Function
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 18th, 2012, 12:10 PM
#3
Thread Starter
New Member
Re: Data Grid View Sorting Help
unfortunately i can only use bubble sort
i haven't learnt ICompare yet
-
Mar 18th, 2012, 12:25 PM
#4
Re: Data Grid View Sorting Help
vb Code:
Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
Dim rows() As DataGridViewRow = dgvolympics.Rows.Cast(Of DataGridViewRow).Where(Function(r) Not r.IsNewRow).ToArray
'bubble sort
For outer As Integer = rows.GetUpperBound(0) To 0 Step -1
For inner As Integer = 0 To outer - 1
If CInt(rows(inner).Cells("Gold").Value) < CInt(rows(inner + 1).Cells("Gold").Value) Then
Dim temp As DataGridViewRow = rows(inner)
rows(inner) = rows(inner + 1)
rows(inner + 1) = temp
End If
Next
Next
dgvolympics.Rows.Clear()
dgvolympics.Rows.AddRange(rows)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 18th, 2012, 12:30 PM
#5
Thread Starter
New Member
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
-
Mar 18th, 2012, 12:37 PM
#6
Re: Data Grid View Sorting Help
read post #2 again. it does the totals
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 18th, 2012, 12:45 PM
#7
Re: Data Grid View Sorting Help
 Originally Posted by MrZubs
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:
Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsort.Click
Dim rows() As DataGridViewRow = dgvolympics.Rows.Cast(Of DataGridViewRow).Where(Function(r) Not r.IsNewRow).ToArray
'bubble sort
For outer As Integer = rows.GetUpperBound(0) To 0 Step -1
For inner As Integer = 0 To outer - 1
If CInt(rows(inner).Cells("Gold").Value) < CInt(rows(inner + 1).Cells("Gold").Value) Then
Dim temp As DataGridViewRow = rows(inner)
rows(inner) = rows(inner + 1)
rows(inner + 1) = temp
ElseIf CInt(rows(inner).Cells("Gold").Value) = CInt(rows(inner + 1).Cells("Gold").Value) Then
If CInt(rows(inner).Cells("Silver").Value) < CInt(rows(inner + 1).Cells("Silver").Value) Then
Dim temp As DataGridViewRow = rows(inner)
rows(inner) = rows(inner + 1)
rows(inner + 1) = temp
ElseIf CInt(rows(inner).Cells("Silver").Value) = CInt(rows(inner + 1).Cells("Silver").Value) Then
If CInt(rows(inner).Cells("Bronze").Value) < CInt(rows(inner + 1).Cells("Bronze").Value) Then
Dim temp As DataGridViewRow = rows(inner)
rows(inner) = rows(inner + 1)
rows(inner + 1) = temp
End If
End If
End If
Next
Next
dgvolympics.Rows.Clear()
dgvolympics.Rows.AddRange(rows)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 18th, 2012, 12:51 PM
#8
Thread Starter
New Member
Re: Data Grid View Sorting Help
the totals part doesn't work
it just puts everything you entered next to each other
-
Mar 18th, 2012, 12:57 PM
#9
Re: Data Grid View Sorting Help
i changed the structure too. + the way the array is filled in btnenter_click
vb Code:
Structure Olympicsinfo
Dim country As String
Dim gold As Integer
Dim silver As Integer
Dim bronze As Integer
Dim total As Integer
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 + 2 = 3 'integer addition
"1" + "2" = "12" 'string concatenation
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 18th, 2012, 01:00 PM
#10
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|