Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/07/2011, 22:43
DianaLopez
 
Fecha de Ingreso: enero-2011
Ubicación: Nicaragua
Mensajes: 11
Antigüedad: 13 años, 10 meses
Puntos: 1
Respuesta: Ordenar fila numérica en un datagridview

Encontré este codigo en la web y sirve genial.. ordena de menor a mayor


Public Class Form1


Private Sub DataGridView1_SortCompare(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewSortCompareEventA rgs) Handles DataGridView1.SortCompare
If Double.Parse(e.CellValue1) > Double.Parse(e.CellValue2) Then
e.SortResult = 1
ElseIf Double.Parse(e.CellValue1) < Double.Parse(e.CellValue2) Then
e.SortResult = -1
Else
e.SortResult = 0
End If
e.Handled = True
Exit Sub
End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'' I call it after I add the rows to my datagridview:


Me.DataGridView1.Sort(Column1, System.ComponentModel.ListSortDirection.Ascending)


'Sortable_Column is the name of the column that I've specified would be sorted Programmatic. I do that with the following line:
Me.Column1.SortMode = DataGridViewColumnSortMode.Programmatic


'One thing I've done since I posted the blog post was to change SortCompare such:
End Sub



Private Sub Grid_SortCompare( _
ByVal sender As Object, ByVal e As DataGridViewSortCompareEventArgs) _
Handles DataGridView1.SortCompare

e.SortResult = CompareEx(e.CellValue1.ToString, e.CellValue2.ToString)
e.Handled = True
Exit Sub
End Sub


'And I added the following function to take care of the manual compare

Private Function CompareEx(ByVal s1 As Object, ByVal s2 As Object) As Integer

Try
' convert the objects to string if possible.
Dim a As String = CType(s1, String)
Dim b As String = CType(s2, String)

' If the values are the same, then return 0 to indicate as much
If s1 = s2 Then Return 0

' Look to see if either of the values are numbers
Dim IsNum1 As Boolean = IsNumeric(a)
Dim IsNum2 As Boolean = IsNumeric(b)

' If both values are numeric, then do a numeric compare
If IsNum1 And IsNum2 Then
If Double.Parse(s1) > Double.Parse(s2) Then
Return 1
ElseIf Double.Parse(s1) < Double.Parse(s2) Then
Return -1
Else
Return 0
End If
' If the first value is a number, but the second is not, then assume the number is "higher in the list"
ElseIf IsNum1 And IsNum2 = False Then
Return -1
' if the first values is not a number, but the second is, then assume the number is higher
ElseIf IsNum1 = False And IsNum2 Then
Return 1
Else
' If both values are non nuermic, then do a normal string compare
Return String.Compare(s1, s2)
End If
Catch ex As Exception
'Console.WriteLine(ex.ToString)
End Try

' If we got here, then we failed to compare, so return 0
Return 0
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To 6
Me.DataGridView1.Rows.Add()
Next
End Sub

'http://tedshelloworld.blogspot.com/2008/05/datagridview-sorting-numeric-columns.html
End Class