Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

Ordenar fila numérica en un datagridview

Estas en el tema de Ordenar fila numérica en un datagridview en el foro de Visual Basic clásico en Foros del Web. Hola gente! :) tengo una fila numérica y quiero ordenarla de mayor a menor, el problema es que se ordena como texto (1,11,12,2,3) y no ...
  #1 (permalink)  
Antiguo 21/07/2011, 21:41
 
Fecha de Ingreso: enero-2011
Ubicación: Nicaragua
Mensajes: 11
Antigüedad: 13 años, 2 meses
Puntos: 1
Ordenar fila numérica en un datagridview

Hola gente! :)
tengo una fila numérica y quiero ordenarla de mayor a menor, el problema es que se ordena como texto (1,11,12,2,3) y no numericamente(1,2,3,11,12).
Alguien sabra alguna funcion o propiedad o algo para que ordene los numeros??
  #2 (permalink)  
Antiguo 21/07/2011, 22:43
 
Fecha de Ingreso: enero-2011
Ubicación: Nicaragua
Mensajes: 11
Antigüedad: 13 años, 2 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
  #3 (permalink)  
Antiguo 21/07/2011, 22:45
 
Fecha de Ingreso: enero-2011
Ubicación: Nicaragua
Mensajes: 11
Antigüedad: 13 años, 2 meses
Puntos: 1
Respuesta: Ordenar fila numérica en un datagridview

y para que ordene de mayor a menor se cambia
Me.DataGridView1.Sort(Column1, System.ComponentModel.ListSortDirection.Descending )
por
Me.DataGridView1.Sort(Column1, System.ComponentModel.ListSortDirection.Ascending)

Etiquetas: datagrid, datagridcolum, datagridview
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:48.