Foros del Web » Programando para Internet » ASPX (.net) »

ordenar gridview por columnas

Estas en el tema de ordenar gridview por columnas en el foro de ASPX (.net) en Foros del Web. hola a todos estoy buscando el codigo para poder ordenar los datos de un gridview al hacer click sobre las cabeceras de la celda. Tambien ...
  #1 (permalink)  
Antiguo 13/11/2008, 14:40
Avatar de fbirrer  
Fecha de Ingreso: enero-2006
Ubicación: Santiago de Chile
Mensajes: 329
Antigüedad: 18 años, 3 meses
Puntos: 0
Mensaje ordenar gridview por columnas

hola a todos

estoy buscando el codigo para poder ordenar los datos de un gridview al hacer click sobre las cabeceras de la celda.

Tambien quisiera mostrar imagen de acuerdo a la columna en la cual esta ordenada el grid view....

¿¿Alguien tiene alguna sugerencia??

Gracias
__________________
Desde Santiago de Chile
Grupo Universite
Información sobre los Institutos Chilenos de Educación
www.universite.cl
  #2 (permalink)  
Antiguo 14/11/2008, 09:37
Avatar de mdavila  
Fecha de Ingreso: julio-2007
Ubicación: Montevideo (Uruguay)
Mensajes: 919
Antigüedad: 16 años, 9 meses
Puntos: 13
Respuesta: ordenar gridview por columnas

No te voy a mentir.... lo realize y me fue dificil hacerlo funcionar correctamente.... te recomiendo que pongas un DropDownList con las opciones de ordenamiento y listo.

Igualmente te tiro como lo realize, aunque es un poco entreverado de entender.


Protected Sub Gr_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gr.RowDataBound
If Page.IsPostBack Then
If e.Row.RowType = ListItemType.Header Then
For Each tc As TableCell In e.Row.Cells
If tc.Controls.Count > 0 And tc.Controls.Count < 2 Then
If Ordenar.Value.ToString.Contains(CType(tc.Controls( 0), System.Web.UI.WebControls.LinkButton).CommandArgum ent) Then
Dim img As New System.Web.UI.WebControls.Image
If HOrden3.Value = "Asc" Then
img.ImageUrl = "arriba.gif"
Else
img.ImageUrl = "abajo.gif"
End If
img.Width = System.Web.UI.WebControls.Unit.Pixel(15)
img.Height = System.Web.UI.WebControls.Unit.Pixel(10)
tc.Controls.AddAt(0, img)
End If
End If
Next
End If
End If
End Sub


Horden3 es un campo oculto que no inicializo en Asc en el page load.


Protected Sub Gr_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles Gr.Sorting
Select Case e.SortExpression.Trim
Case "Fecha"
Ordenar.Value = "UsuariosPostulados.FechaPostula"
HIndicePintar.Value = 1
IndicePintar = 1
Case "Nombre"
Ordenar.Value = "Usuarios.Nombre"
HIndicePintar.Value = 2
IndicePintar = 2
Case "Apellido"
Ordenar.Value = "Usuarios.Apellido"
HIndicePintar.Value = 3
IndicePintar = 3
Case "Telefono"
Ordenar.Value = "Usuarios.Telefono"
HIndicePintar.Value = 4
IndicePintar = 4
Case "Mail"
Ordenar.Value = "Usuarios.Mail"
HIndicePintar.Value = 5
IndicePintar = 5
Case "Pais"
Ordenar.Value = "Usuarios.Pais"
HIndicePintar.Value = 6
IndicePintar = 6
End Select
HOrden2.Value = "True"
Gr.PageIndex = 0
LlenarGrilla(Ordenar.Value)
End Sub


'cuando se crea la grilla
Protected Sub Gr_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gr.RowCreated
Dim itemType As ListItemType

'esto es para que cuando pase el mouse por arriba las celdas cambien de color
If e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Pager Then

e.Row.BackColor = Color.FromName("#EEEEEE")

If (itemType <> ListItemType.AlternatingItem) Then
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'Gainsboro';")
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';")
Else
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'Gainsboro';")
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';")
End If
End If

If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(IndicePintar).BackColor = Color.FromName("#760205")

End If

End Sub




Mucha suerte y vas a tener que cambiar cosas porque son personales de mi proyecto ok.
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 13:57.